Error: The Gravatar service is unable to retrieve the property 'get' from an undefined source

I have a basic app with just one controller. I'm trying to debug the output of the Gravatar.get() function in the console.

However, I encounter an error saying: "TypeError: Cannot read property 'get' of undefined". I'm confused because I've injected the service into both my module and controller. Am I calling the Gravatar.get() function correctly?

Below, you can find a snippet of my code along with the gravatar.js file containing the get() function.

//****************************************
//******** My simple controller**********

var main = angular.module('firebaseConfig', ['firebase', 'gravatar']);

main.run(function(){

  // Initialize Firebase
  //*****
  //*****
  //*****
  //*****

});

main.controller("Ctrl", ['$scope', '$firebaseArray', '$ionicPopup', '$timeout', '$ionicLoading', '$state', '$ionicHistory', 'Gravatar', function ($scope, $firebaseArray, $ionicPopup, $timeout, $ionicLoading, $state, $ionicHistory,  $cordovaImagePicker, Gravatar) {

 var grav = Gravatar.get('[email protected]', 100);
 console.log("Grav value:", grav);
 
}
//****************************************

// GRAVATAR SERVICE JS FILE :

angular.module('gravatar', [])

.service('Gravatar', [function(){
   
    // MD5 calculation function and other methods omitted for brevity
    
    var ret = {
        'get': function(email, size){
            if (!email) return;
            size = size || 80;
            return 'https://www.gravatar.com/avatar/' + MD5(email) + '.jpg?s=' + size;
        }
    }
    
    return ret;

}])

.directive('gravatarSrc', [
  'Gravatar',
function(Gravatar) {

  return {
          restrict: 'A',
          link: function(scope, element, attrs) {
            var watchVal = attrs['gravatarSrc'];
            var watchSize = parseInt(attrs['gravatarSize']) || 80;
            scope.$watch(watchVal, function(val) {
              element.attr('src', Gravatar.get(val, watchSize));
            });
          }
        };
}]);

Answer №1

Make sure to include the $cordovaImagePicker injection in your code:

main.controller("Ctrl", ['$scope', '$firebaseArray', '$ionicPopup', '$timeout', '$ionicLoading', '$state', '$ionicHistory', '$cordovaImagePicker', 'Gravatar', function ($scope, $firebaseArray, $ionicPopup, $timeout, $ionicLoading, $state, $ionicHistory, $cordovaImagePicker, Gravatar) {

  var grav = Gravatar.get('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="36534e575b465a53[protected]', 100);
  console.log("Grav is equal to:", grav);

}

Answer №2

It appears that you forgot to include $cordovaImagePicker as a dependency. As a result, the service Gravatar is now associated with the $cordovaImagePicker object in the controller.

To fix this issue, make sure to include $cordovaImagePicker in your dependencies list:

main.controller("Ctrl", ['$scope', '$firebaseArray', '$ionicPopup', '$timeout', '$ionicLoading', '$state', '$ionicHistory', '$cordovaImagePicker', 'Gravatar',
 function ($scope, $firebaseArray, $ionicPopup, $timeout, $ionicLoading, $state, $ionicHistory,  $cordovaImagePicker, Gravatar) {

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Saving the author of a message from one function and transferring it to another

I'm currently working on a Discord bot that manages tickets as applications. I've almost completed it, but I want the bot to log the closed ticket when the -close command is used. I've experimented with different approaches, such as using a ...

How to activate a single element within a React array

I am currently working on developing a comment system similar to the one found on YouTube. In my setup, when I click on modify, all comments turn into input fields, but only the selected input should be modified. How can I trigger only the element I clicke ...

Trouble with Click event not working in Electron and mouse cursor not changing when hovering over an HTML button?

I'm in the midst of a project that involves using the electron framework. Within my project, I have an HTML file, a CSS file, and a Javascript file. The issue at hand is that when I hover over a button, the expected hand pointer icon fails to appear d ...

When using React / Next.js, the .map() function may not rerender the output based on changes in the state array if the keys remain the same

In my react component, Matches.js, I handle the display of tournament matches sorted by rounds. The data is fetched from a parent component through nextjs SSR and passed as props to the child component. To avoid unnecessary requests upon data changes like ...

The issue of the menu in the <Select /> component unexpectedly closing is caused by the MaterialUI -withStyles higher-order component (HOC)

Issue Description When utilizing the <Select /> component with the multiple attribute, users should be able to select multiple options without the dropdown menu closing. This functionality works as intended when using the <Select /> component ...

Creating a dropdown menu utilizing JavaScript/jQuery arrays

Looking to create an HTML select menu using a JavaScript array where the keys are used as values for options. The challenge is when entering numbers as keys, they should be accepted in the code. a([selected="No of rooms", 1="1", 2="2", 3="3", 4="4", 5="5" ...

The process of including a property in Vue.JS

Looking to include this property on my button: uk-toggle="target: #id" The desired outcome is: <button uk-toggle="target: #id" type="button">Click</button> I'm trying to achieve this with Vue.JS but I'm facing some difficulties. H ...

Getting a specific index from an array using the Angular ng-repeat Directive: A step-by-step guide

I am trying to retrieve a specific index in an array using the ng-repeat directive. Currently, it is displaying information for all indexes... I only want to display the information for the second index as an example... This is my main.js: app.controll ...

Issues with the code: $("input[type=checkbox]:checked").each(function() { var checkboxId = $(this).attr("id"); });

When submitting a form, I need to retrieve the IDs of all checked checkboxes. Currently, $(this).id() is causing an error. What is the correct code to obtain the IDs of all checked checkboxes? $("#pmhxform input:checkbox:checked").each(function() { ...

"Encountering a strange issue where submitting a form with Jquery and AJAX in Rails does not work as expected, causing the index

Currently facing a unique issue with a jQuery/Ajax HTML update form. The application in question is a single-page TODO App that utilizes a Rails controller, where all changes to the DOM are made through jQuery/Ajax. After rendering the TODOs on the page v ...

Has the zip creation tool in the Google Doodle on April 24th been coded entirely in JavaScript?

Was the Gideon Sundback Google doodle created using JavaScript? I attempted to follow along in Firebug, but I couldn't quite grasp its implementation details. Any ideas on how it was possibly implemented? Any insights on the potential techniques use ...

Encountering the "TypeError: Unable to access property 'indexOf' of undefined" error while utilizing the ipfs-api

During my development work with the ipfs-api, I ran into an issue where adding an image file to the ipfs node was not functioning properly. Upon further investigation into the error details, it appears that the protocol is being treated as undefined in the ...

Ensure that the array in Jest does not have any falsy values present

Attempting to utilize Jest for a unit test to ensure the absence of falsy values in my array named values. Unfortunately, the initial approach is not effective; the test actually passes: const badValues = ['', null, undefined, false, {}, []]; e ...

Numerous Axios GET calls made by various components

I am facing an issue where I have two different components rendering on the main screen. Both of them make multiple axios.get requests to fetch data. However, upon initial page load, only the last component successfully retrieves the data while the first c ...

What is the best way to customize the spacing of grid lines in chartist.js?

I am struggling with chartist.js. I want to increase the spacing between y-axis gridlines by 40px. (Currently set at 36px) I have tried looking for examples, but haven't found any. .ct-grids line { stroke: #fff; opacity: .05; stroke-dasharray: ...

Using JavaScript to open links in a new tab with the target

document.getElementById("mahacareer").onclick = function () { window.open("http://www.mahacareermitra.in", '_blank'); }; <a href="" id="mahacareer">Access the portal</a> Hi there, I am looking to have the link above open in a new tab ...

The ng-include feature seems to be malfunctioning

After building a basic web page using AngularJs, I ran into an issue when attempting to integrate header.htm into index.html - nothing was displaying in the browser. index.html <html> <script src="https://ajax.googleapis.com/ajax/libs/angul ...

Executing code asynchronously and handling callbacks using Process.nextTick and Promise

When I execute the following code: process.nextTick(() => console.log(8)) Promise.resolve("hi").then(t => console.log(t)) console.log(7); The output displayed is 7 8 hi This behavior is as expected because process.n ...

Manipulate an image by hiding it, altering its contents, and then displaying it

I am seeking the most effective method to accomplish the following task: $("#some-image").fadeOut(); $("#some-image").attr("src", "new-src.png"); $("#some-image").fadeIn(); For timing purposes, the code below is a step closer, but it still needs improvem ...

Working with Parse.com's REST API: Implementing skip and limit queries using JavaScript

Currently, I am in the process of retrieving data using Parse through the REST API in Javascript. Everything is working fine until I attempt to include a limit and an offset ("skip"). Here, you can find the documentation for using parameters with a REST ...