How do I use PUT to replace an entry?

32 views
Skip to first unread message

donpe...@gmail.com

unread,
Apr 21, 2017, 7:10:30 AM4/21/17
to LoopbackJS
Hi, I struggle a bit with a basic PUT, but I cannot find any information about my problem. If use PUT to add an entry everything works fine, I just pass my data and there is a new entry in the database. Now I want to replace an entry, as described in the documentation for PUT (I am using the API explorer for my tests). My idea was to pass an 'id' (in addition to the changed data) and loopback would replace the corresponding entry. But that does not work, I get the error:

The `Model` instance is not valid. Details: `id` can't be set

How can I use PUT to replace an entry?

Best,
Peter

Francois Laforge

unread,
Apr 24, 2017, 11:12:54 AM4/24/17
to LoopbackJS
Could you provide a link where the documentation says that a PUT verb can be used to replace an entity?  Thanks!

donpe...@gmail.com

unread,
Apr 26, 2017, 4:44:26 AM4/26/17
to LoopbackJS
Whenn I open the Loopback explorer the PUT method has a documentation:

"Replace an existing model instance or insert a new one into the data source"

Francois Laforge

unread,
Apr 27, 2017, 10:00:53 AM4/27/17
to loopb...@googlegroups.com
I think that if you include the ID in the url, you are replacing properties of an existing model:

PUT ~/modelName/{id}/
{
 
// model data goes here.  Everything here updates in the database, but the ID stays the same
}


If you include the ID in the body of the PUT, loopback will attempt to update the model or create a new one if it does not find it:

PUT ~/modelName/
{
  id
: 'instance id' // update existing model with new data or create new model with this ID
 
// rest of the data goes here
}


When the documentation says "replace an existing model" what I they mean is "update an existing mode's data, but keep the ID".  There is no method to create a new model with a new id and delete an old one all in a single command.

I hope that helps.
Reply all
Reply to author
Forward
0 new messages