Answer the question
In order to leave comments, you need to log in
Why do we need put and delete if there is findOneAndDelete and findOneAndUpdate?
I would like to know if it would be easier to use the post and get methods than to bother with put and delete? Below I will give an example of code where, using the post method, I delete and change the data in the collection.
app.post('/:id', async (req, res) => {
const filter = req.params.id;
const update = { text: 'changed1' };
await Test.findOneAndUpdate(filter, update, {
new: true
});
res.redirect('/')
})
app.post('/:id', async (req, res) => {
const id = req.params.id;
Test.findOneAndDelete({_id: id}, function(err, doc){
mongoose.disconnect();
if(err) return console.log(err);
});
res.redirect('/')
})
Answer the question
In order to leave comments, you need to log in
They have different semantics:
The get method implies that no changes will be made to the server (a safe operation)
The post method allows you to put some data on the server and returns a link to it (if something is created)
The put method allows you to place data in advance known path
The delete method allows you to delete data along a known path
Plus, sending multiple post requests will create multiple objects. And the effect on the server of sending multiple put or delete requests is always equal to the effect of a single put or delite request (if the requests are identical). This property is called idempotency..
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question