Saving a new record, how should JSON API response look like?


#1

I recently switched to JSON API but I run into trouble when saving a new record. Say I created a record of the model type foobar and save it:

let record = this.get('store').createRecord('foobar', {
  name: 'whheeeeee'
});
record.save().then(function(record) {
  console.log('record saved: ' + record.get('id'));
});

And the server reponse looks like this:

{
  'data': [
    {
      'type': 'foobar',
      'id': 'foobar_id_123'
    }
  ]
}

No error of whatsoever, but apparently the response is not synced with the actual record in the store. The ID of the record is still null!

It worked in REST. So how do I fix this?


#2

Should data be an array? If it’s a single object, I would think it wouldn’t be. Take a look at the example here

{
  "data": {
    "type": "photos",
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "attributes": {
      "title": "Ember Hamster",
      "src": "http://example.com/images/productivity.png"
    }
  }
}

#3

Wow, yeah, that did it. I feel kinda embarrassed, I’d better quit for today. Thanks. :slight_smile: