reyesoft / ngx-jsonapi

JSON API client library for Angular 5+ 👌 :: Production Ready 🚀
https://ngx-jsonapi.reyesoft.com/
MIT License
101 stars 52 forks source link

Cachememory - Cannot read property 'data' of undefined #184

Closed baranchikovaleks closed 4 years ago

baranchikovaleks commented 5 years ago

Hi there! It seems like cachememory is not doing well when relationships are added/deleted. Here is my case.

A resource has 2 mutually exclusive relationships, so when we set one we should remove another. According to JSONAPI spec we can do it via PATCH with relation.data = null . So I do resource.save() which results in the following request to the server

{
  "data": {
    ...
    },
    "relationships": {
      "relation1": {
        "data": {
          "id": "541",
          "type": "first-relation"
        }
      },
      "relation2": {
        "data": null
      }
    }
  }
}

and got response from server (according to the spec The response document MUST include a representation of the updated relationship(s).)

{
  "data": {
    ...
    },
    "relationships": {
      "relation2": {
        "data": null,
      },
      "relation1": {
        "data": {
          "id": "541",
          "type": "relation1"
        }
      }
    }
  },
  "included": [
      {
      "id": "541",
      "type": "relation1",
      "attributes": {
         ...
      }
    }
  ]
}

And finally I've got the following error

core.js:15724 ERROR TypeError: Cannot read property 'data' of undefined
    at CacheStore.push../node_modules/ngx-jsonapi/ngx-jsonapi/@ngx-jsonapi/ngx-jsonapi.es5.js.CacheStore.setCollection (ngx-jsonapi.es5.js:1551)
    at SafeSubscriber._next (ngx-jsonapi.es5.js:2157)
    at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:196)
    at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next (Subscriber.js:134)
    at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next (Subscriber.js:77)
    at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
    at CatchSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next (Subscriber.js:77)
    at CatchSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
    at TapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/tap.js.TapSubscriber._next (tap.js:51)
    at TapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js

It looks like it tries to update cachememory and fails, because data is null.

Is there a way to completely disable caching, including cachememory? (#174 still has no answer)

baranchikovaleks commented 5 years ago

Hello there,

Is this repo still alive?

dimas commented 5 years ago

@maxi7587, @pablorsk are you guys aware of this issue? Feels like incompatibility with jsonapi spec to me (although admittedly, for an edge case)

pablorsk commented 4 years ago

LIbrary now support on v2.1.x version:

:tada: