Canttouchit
Canttouchit

Reputation: 3159

AngularJS, Getting error when trying to use $resource

My code: http://plnkr.co/edit/2blxwwyv0gS9GYui7IVn?p=preview

I defined a service:

angular.module('jsonService', ['ngResource']).factory('JsonService', function($resource) {

     var jsonService = $resource('data.json/:id',
          {id: "@id"}, //parameters default
          {
            getAll: { method: "GET", params: {} },
            addNew: { method: "POST", params: { id: ":id"}},
          });
     return jsonService;

  });

I keep getting error when I try to call getAll from my controller. I also tried to add a new object but AddNew simply would not work from the controller.

Upvotes: 0

Views: 85

Answers (1)

zs2020
zs2020

Reputation: 54514

Add isArray: true

getAll: { method: "GET", params: {}, isArray: true },

Please take a look at actions parameter from $resources.

isArray – {boolean=} – If true then the returned object for this action is an array, see returns section.

And this is how to post data

non-GET "class" actions: Resource.action([parameters], postData, [success], [error])

For example:

var obj = { "id": "2", "created": "3424324", "updated": "2342666", "name": "Bob" };
JsonService.addNew({ "id": "2"}, obj)

Upvotes: 2

Related Questions