Sam R.
Sam R.

Reputation: 16450

$resource.get gives "TypeError: undefined is not a function"

What's wrong with this simple Angular app:

// app.js
(function () {
    'use strict';

    angular.module('app', [

        'ngRoute',
        'ngResource',
        'app.weather'

    ])
        .config(['$routeProvider', function ($routeProvider) {
            $routeProvider.otherwise({redirectTo: '/'});

    }]);
}());

And the controller and resource:

// weather.js
(function () {
    'use strict';

    // Resource
    angular.module('app.weather',['ngResource'])
        .factory('Entry', function($resource) {
            return $resource('http://api.openweathermap.org/data/2.5/weather');
        });

    // Controller
    angular
        .module('app.weather')
        .controller('Weather', Weather);

    Weather.$inject = ['$resource'];

    function Weather(Entry) {
        var vm = this;
        var result = Entry.get({q: "London,uk"}, function() {
            vm.data = result;
            console.log(result);
        });
    }

}());

It gives the following error which refers to Entry.get({q: "London,uk"}... line:

TypeError: undefined is not a function
    at new Weather (weather.js:25)
    at Object.invoke (angular.js:4185)
    at $get.extend.instance (angular.js:8454)
    at angular.js:7700
    at forEach (angular.js:331)
    at nodeLinkFn (angular.js:7699)
    at compositeLinkFn (angular.js:7078)
    at compositeLinkFn (angular.js:7081)
    at compositeLinkFn (angular.js:7081)
    at publicLinkFn (angular.js:6957)

I have checked the other questions about this but I couldn't catch the problem.

Upvotes: 2

Views: 932

Answers (1)

dfsq
dfsq

Reputation: 193261

Dependency injection is not correct, you should be injecting Entry service, not $resource:

Weather.$inject = ['Entry'];

Upvotes: 1

Related Questions