frank06 / ember-data-save-relationships

A mixin for Ember Data JSON API serializers to save relationship data
https://github.com/laborvoices/ember-data-save-relationships
MIT License
41 stars 20 forks source link

Unable to serialize when has many relationship is empty #16

Open stoicsquirrel opened 8 years ago

stoicsquirrel commented 8 years ago

The following error occurs in addon/index.js, line 21 when no items have yet been added to a has many relationship:

Uncaught TypeError: Cannot read property 'map' of undefined
if (relKind === "hasMany") {
  data.relationships[key].data = snapshot.hasMany(relKey).map(this.serializeRecord);
}

I am unsure as to why an undefined, rather than empty, object is being returned when calling snapshot.hasMany(relKey) on an empty relationship, but I added a test for undefined and it worked in my example. I was unable, though, to replicate this issue in a test, and I am unsure if this is the right solution to the problem.

if (relKind === "hasMany" && typeof(snapshot.hasMany(relKey)) !== "undefined") {
  data.relationships[key].data = snapshot.hasMany(relKey).map(this.serializeRecord);
}

Let me know if you need more info.

frank06 commented 8 years ago

Thanks for reporting the issue! I will see what test I can add for that and fix it.