Skip to content

Errors when response has empty relationships #3

Description

@andrepcg

In my backend I have a conversation model that olds a foreign_key to the last sent message in that conversation. If there is no last message, this normaliser fails.

Example:

import normalize from 'normalize-json-api-response';

const json = {
   "data":[
      {
         "id":"3",
         "type":"conversation",
         "attributes":{
            "created_at":"2019-09-19T10:03:44.401Z",
            "subject":"Test subject"
         },
         "relationships":{
            "last_message":{
               "data":null
            }
         }
      },
   ],
   "included":[]
};

normalize(json)

Uncaught (in promise) TypeError: Cannot read property 'constructor' of null at normalize (index.js:57) (

if (relation.data.constructor === Array) {
)

I also had occasions where it would fail on line 53 (

if (!relation.data[0].hasOwnProperty('type')) {
) when relation.data[0] would be undefined.

I have temporarily fixed this in the backend by not serializing fields that are empty but that's going to be cumbersome to do across all serializers.


Also, if the serializer does not include any relationship, and thus the included attribute does not exist, the normalizer crashes as well (

response.included.find((item: any) => (String(item.id) === String(temp.id)) &&
). Do you plan on supporting these "features"? I can fork the repo use that if it's something you don't want to support

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions