AngularJS $promise is undefined
Asked Answered
P

3

3

I am trying to make an ajax call using $resource and load a datatable upon receiving the data from a server. But when I call a get() then I am getting $promise as undefined. I am using a factory to make the call. Here is my factory :

    app.factory('getAllUsers', [ '$resource', function($resource) {
return $resource('URL', {}, {get : {method : 'GET'}});
}]);

And the controller :

      app.controller('MyController',function($scope,$location,getAllUsers){

        console.log("In controller");

        getAllUsers.get().$promise.then(function(data) {

            loadDatatable(data);
            });


     });

ERROR : getAllUsers.get().$promise is undefined

NOTE : I have included ngResource in my app and angular-resource.js in index.html too.

Perceptive answered 25/10, 2013 at 11:49 Comment(0)
P
1

The $resource service doesn't return promises in version 1.0.x; be sure to read the correct version of the docs. By default, http://docs.angularjs.org/api/ is for the 1.2.x series, which is currently unstable. Try this version of the api docs: http://code.angularjs.org/1.0.8/docs/api

Perspex answered 25/10, 2013 at 14:18 Comment(0)
B
0

Since you are using $resource you can do

 getAllUsers.get({},function(data) {
    loadDatatable(data);
 });
Barboza answered 25/10, 2013 at 12:29 Comment(0)
F
0

Did you forget to add .json as type of call?

app.factory "User", ["$resource", ($resource) ->
  $resource("/users/:id.json", {id: "@id"}, {update: {method: "PUT"}})
]
Footcandle answered 24/9, 2014 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.