Search code examples
angularjsexpressangular-resource

AngularJS service with express error: [$resource:badcfg] object


I'm using external mongodb and wrote a node/express server for fetching the data on my localhost.

I query localhost like this:

http://localhost:8888/api/bonsais

And I'm getting the correct results from my mongodb collection @ mongolab.

[{"name":"test,test2","_id":"536be2e2ae54668818000001","__v":0},{"name":"testname","_id":"536fd2df41f84a581c000001","__v":0}]

I wrote a service to fetch the data like this:

angular.module('bonsaiService', ['ngResource']).
  factory('bonsaiService', function($resource) {
  return $resource('http://localhost:8888/api/bonsais',{'query': {method: 'GET', isArray: true }});
});

I'm getting a Error: [$resource:badcfg] object, which is referring to these error docs


Solution

  • .factory('Bonsai', function($q, $resource){
        var bonsaiResource = $resource('http://localhost:8888/api/bonsais', {}, {
            get: {
                method: 'GET',
                isArray: true
            }
        });
    
        return {
        get: function() {
          var q = $q.defer();
    
            bonsaiResource.get({
            },
            function(resp) {
                q.resolve(resp);
            }, function(httpResponse) {
                q.reject(httpResponse);
            });
    
            return q.promise;
        } };
    })
    

    try to write it this way. Then you can call it with Bonsai.get()