How to update model with returned uuid from API

I am sending a request to create a new record to an API. This call is successful and returns a uuid for the newly created record. I want to update the local store with the uuid. I am using normalizeResponse is my serializer like this:

normalizeResponse(store, primaryModelClass, payload, id, requestType){ if (requestType === ‘createRecord’) { console.log(payload.uuid); // this shows the uuid being returned from the API

} return this._super(store, primaryModelClass, payload, id, requestType); }

I would have expected the store to update the model (uuid exists in the model) but it does not update. What do I need to add to make the model update with the returned uuid? Note: the uuid is NOT the primary key in the record.

Which serializer base class are you using?

ApplicationSerializer, adapter is ApplicationAdapter. Thanks

Ah, right, but then the question is which base class is the ApplicationSerializer using? JSONAPISerializer?

Yes, JSONSerializer.

Ok, then the value you return from normalizeResponse needs to be a valid json:api response.

So if your server is just returning { uuid: 'xxxxx' }, you would structure it to look like valid jsonapi:

normalizeResponse(store, primaryModelClass, payload, id, requestType){ 
  if (requestType === ‘createRecord’) { 
     console.log(payload.uuid); // this shows the uuid being returned from the API
     payload = {
        data: {
           type: 'my-model-type',
           id,
           attributes: { 
              uuid: payload.uuid
           }
        }
     }
  }
  return this._super(store, primaryModelClass, payload, id, requestType); 
}
2 Likes

That’s great, thank you