0

The find method in js-data-http appears to have a special case where if item is falsey then it will reject the promise, otherwise it returns item. My problem is that some falsey values (0, null, '') are valid responses from my API.

For example if I ask for a relation that hasn't been set then the API responds with data null wrapped in a jsonapi envelope. This follows the jsonapi spec for fetching empty relationships:

{
  "links": {
    "self": "/articles/1/relationships/author",
    "related": "/articles/1/author"
  },
  "data": null
}

I'm using the js-data-jsonapi library to help js-data and jsonapi get along. In that library the DeSerializer unwraps the jsonapi envelop. At that point js-data is back in charge and the unwrapped data null is the value for item in the find promise resolve method which causes js-data-http to reject the promise.

Right now I'm taking advantage of a special case in js-data-jsonapi to deserialize null data as an array because in JS Arrays are truthy. But then I must special case the detection for hasOne relations where now an empty array must be re-converted into null.

This seems like an overly complicated way of handling things, is there a better way that I am missing?

My setup uses:

"angular": "1.5.8",
"js-data": "2.9.0",
"js-data-angular": "3.2.1",
"js-data-jsonapi": "0.0.0-alpha.21",
Rob
  • 3,687
  • 2
  • 32
  • 40
  • It seems that [js-data](https://github.com/js-data/js-data/blob/2.9.0/src/datastore/sync_methods/inject.js#L315) and [js-data-http](https://github.com/js-data/js-data-http/blob/2.2.2/src/index.js#L238) both care strongly about the _falsinisity_ of the response from the server. – Rob Jan 30 '17 at 14:51
  • Can you provide details such as what version of js-data and js-data-http you're using? Note that `find` no longer throws on falsy values in JSData 3.x – jdobry Feb 01 '17 at 19:18
  • I'm not exactly sure how js-data-http is involved, the error I get is stacktraced to js-data-angular, but in the repos the error message ("Not Found!") doesn't exist in js-data-angular, but it does in js-data-http so I assume there is some build magic. – Rob Feb 01 '17 at 21:43
  • @Rob your stacktrace probably looks like it's coming from js-data-angular because that's where the promise resolves/catches. Stacktraces when promises are involved can be illusive. – Cory Robinson Feb 01 '17 at 22:25
  • @CoryRobinson that could be the case, but I'm not actually using js-data-http in my app. At least not directly installed by npm. I think I have js-data setup to use Angular's $http. – Rob Feb 02 '17 at 14:51

0 Answers0