beauby / jsonapi-datastore

JavaScript client-side JSON API data handling made easy.
MIT License
195 stars 40 forks source link

store crashes when serializing null relationship #12

Closed jamesdixon closed 8 years ago

jamesdixon commented 8 years ago

When attempting to serialize an object with a null relationship, the serialize() method crashed. I was going to make a PR, but my fork is already a bit customized to serialize the store to an object that doesn't contain any of the store methods so I can pass it to my ORM.

That said, I've tracked the issue down to this line. Essentially, self[key] is undefined when a null relationship is serialized, which causes the crash. This bit of code appears to fix it:

    if (self[key]) {
        if (self[key].constructor === Array) {
          res.data.relationships[key] = {
            data: self[key].map(relationshipIdentifier)
          };
        } else {
          res.data.relationships[key] = {
            data: relationshipIdentifier(self[key])
          };
        }
      } else {
        res.data.relationships[key] = null;
      }

I haven't fully tested, but just wanted to give a heads up. Thanks for building a great library!

beauby commented 8 years ago

Thanks for the heads up, will fix ASAP.