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

"the response does not have an id" error being thrown on response from server #25

Closed jrdn91 closed 6 years ago

jrdn91 commented 6 years ago

the response does not have an id and your record does not either. Is being thrown on the POST response to my server

I have a model for a tag

export default DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  template: DS.attr('number')
});

Then I want to save a bunch at once so I created a bunch of these tag records and have a taglist record with a relationship

export default DS.Model.extend({
  modelList: DS.hasMany('tag')
});

Which also has a serializer

export default DS.JSONAPISerializer.extend(SaveRelationshipsMixin, {
  attrs: {
    modelList: { serialize: true }
  }
});

I create this record like so

const modelList = tags.map((tag) => {
  return this.store.createRecord('tag', {
    firstName: tag[firstNameKey],
    lastName: tag[lastNameKey],
    template: 1
  });
});
const tagList = this.store.createRecord('taglist', {modelList: modelList});
tagList.save().then(()=>{
  this.get('notify').success('Tags Created',{
    closeAfter: 5000
  });
});

Then my response from the POST of my server when I call .save() on this record looks like this

POST response /taglist
{
  "data": [{
    "type": "tags",
    "id": "5aaa7152a243550bbef02c9b",
    "attributes": {
      "firstName": "**",
      "lastName": "**",
      "template": 1,
      "__id__": "5internal-model"
    }
  }, {
    "type": "tags",
    "id": "5aaa7152a243550bbef02c9d",
    "attributes": {
      "firstName": "**",
      "lastName": "**",
      "template": 1,
      "__id__": "7internal-model"
    }
  }, {
    "type": "tags",
    "id": "5aaa7152a243550bbef02c9c",
    "attributes": {
      "firstName": "**",
      "lastName": "**",
      "template": 1,
      "__id__": "6internal-model"
    }
  }, {
    "type": "tags",
    "id": "5aaa7152a243550bbef02c9e",
    "attributes": {
      "firstName": "**",
      "lastName": "**",
      "template": 1,
      "__id__": "8internal-model"
    }
  }]
}

Which is also what a general GET to /tags from my server looks like minus the __id__ attribute of course.

Just for clarity

GET response /tags
{
  "data": [{
    "type": "tags",
    "id": "5aaa71326de48b0b9e6297b1",
    "attributes": {
      "firstName": "**",
      "lastName": "**",
      "template": 1
    }
  }, {
    "type": "tags",
    "id": "5aaa71326de48b0b9e6297b2",
    "attributes": {
      "firstName": "**",
      "lastName": "**",
      "template": 1
    }
  }, {
    "type": "tags",
    "id": "5aaa71326de48b0b9e6297b3",
    "attributes": {
      "firstName": "**",
      "lastName": "**",
      "template": 1
    }
  }, {
    "type": "tags",
    "id": "5aaa71326de48b0b9e6297b4",
    "attributes": {
      "firstName": "**",
      "lastName": "**",
      "template": 1
    }
  }]
}

Why am I getting this Ember error?

frank06 commented 6 years ago

Hi Jordan. Please follow the official repo at: https://github.com/laborvoices/ember-data-save-relationships