Description
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)
(
Normalize-JSON-API/src/index.ts
Line 51 in 7443bb3
I also had occasions where it would fail on line 53 (
Normalize-JSON-API/src/index.ts
Line 53 in 7443bb3
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 (