Skip to content

save() is not updating the model when server returns 200 #150

@hpawe01

Description

@hpawe01

Hello,

when I update an existing model, the returned value does not contain the updated resource from the server, but the same model from before:

// ...
console.log(comment.updatedAt); // '2017-04-06T04:32:46.919Z'
console.log(comment.rating); // '3'

comment.rating = 5;
comment.save().subscribe(updatedComment => {
  console.log(updatedComment.updatedAt === comment.updatedAt); // true but should be false
  console.log(updatedComment.rating); // '5'
});

My backend changes the updatedAt property of comment and returns the updated resource with the status 200 OK, but the updated comment is not returned by the save method.

The problem came with this commit:

.map((res: any) => this.extractRecordData(res, modelType, model)) // old

.map(res => res.status === 201 ? this.extractRecordData(res, modelType, model) : model) // new

Since then, only data from 201 Created responses gets extracted. But as described in http://jsonapi.org/format/#crud-updating-responses, a PATCH request can respond with 200 OK or 204 No Content (and 202 Accepted).
So if the server responds with 200 OK we also should call this.extractRecordData(res, modelType, model) to update the model with the updates from the server. Further it is possible that the 200 OK does only contain meta information but no updated resource (data). This should be handled in the extractRecordData method as well.

I will create a pull request to resolve this issue. Does anyone has some other suggestions how to solve this problem?

Cheers

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions