angular-restmod icon indicating copy to clipboard operation
angular-restmod copied to clipboard

Record $decode expected an object

Open laszl0 opened this issue 10 years ago • 3 comments

Hello, this project is fantastic, thank you for the creators, they saved lots of hours that i can use for other things. Thanks.

Today i started using it and after spending half a day with it, my first issue arrived. I try to use the find to get the data for a resource

$scope.post = Post.$find($scope.postId);

and i get the error:

Record $decode expected an object

I made a code snipped to replicate the problem. Here it is http://codepen.io/anon/pen/LNpMXK

Forgot to say that my response for the resource from the api is a raw resource object.

{ "userId": 1, "id": 1, "title": "sunt aut ", "body": "quia et " }

My findings.

I traced the issue to this method:

return restmod.mixin(function() {
  this.define('Model.unpack', function(_resource, _raw) {
      var name = null,
          links = this.getProperty('jsonLinks', 'included'),
          meta = this.getProperty('jsonMeta', 'meta');
        ...
        ...
      return _raw[name]; // here it returns undefined
    });

and on the return line, the name has a value that is not present as property on the _raw object. I have used the following:

jsonRootSingle: '.' // but of course this is sily

Hack to get it working.

restmodProvider.rebase({
      $config: {        
        jsonRootSingle: '_single', // hack
      },
      $hooks: {
        'after-request' : function (_res) {
          // this hack is needed because $find is giving the error...
          // ...Record $decode expected an object
          // comment out the line below to get the error
          _res.data = Array.isArray(_res.data) ? _res.data : {_single: _res.data};
        }
      }
    });

laszl0 avatar Mar 05 '16 20:03 laszl0

had the same problem with $save() and this hack works, but breaks $search().

any suggestion?

2624789 avatar Aug 27 '16 16:08 2624789

Found my problem was related with the api response, modified it and everything works ok.

It seems this error is related to the api, not the restmod angular.

2624789 avatar Aug 27 '16 17:08 2624789

Great to hear that you solved your problem :). As i said above, my problem was with "raw resource object".

laszl0 avatar Aug 27 '16 17:08 laszl0