MadPhysicist MadPhysicist - 5 months ago 38
AngularJS Question

AngularJS TypeError: $resource is not a function

I have the following controller that is giving me trouble in the form of


TypeError: $resource is not a function


pointing to
var Activities = $resource('/api/activities');


app.controller('AddActivityCtrl',['$scope','$resource','$location','$rootScope',
function($scope, $rootScope, $resource, $location){
console.log($scope.user.email);
//$scope.activity.user = $rootScope.user.email;
$scope.save = function(){
var Activities = $resource('/api/activities');
$scope.activity.events = [];
Activities.save($scope.activity, function(){
$location.path('/')
})
}
}
]);


It used to work well, but I just tested this feature and something I have done since I last tested it seems to have broken it. What could be the cause of this?

The following comes from app.js

var activities = require('./routes/activities');
app.use('/api/activities', activities);


And in my activities.js I have

//API point for listing all existing activities
router.get('/', function(req, res){
var collection = db.get('Activity');
//collection.find({}, function(err, activities){
collection.find({user:req.user.email}, function(err, activities){
if (err) throw err;
res.json(activities);
});
});

...

Answer

The issue seems to be with order of injection

app.controller('AddActivityCtrl',['$scope','$resource','$location','$rootScope',
    function($scope, $rootScope, $resource, $location){

You are injecting $resource as second service but passing $rootScope as second parameter.

Order of injection and arguments must match

['$scope','$resource','$location','$rootScope',
        function($scope, $resource, $location, $rootScope){..}