patch 请求时,关于id的报错问题

时间:2023-03-09 19:12:57
patch 请求时,关于id的报错问题

在更新操作时,先显示要更新的数据内容,再修改

在发送patch请求时出现

After applying the update to the document {_id: ObjectId('55be3c8f79bae4f80c6b17f8') , ...}, the (immutable) field。。。



原因在于,
  提交修改的数据中会包括数据库生成的特定id,这个id也会随请求发送给服务器,即使没有修改一旦传入即认为你修改了此id
  所以最好的办法就是在服务端,把这个id在传过来的数据中剔除掉即可, express 下
delete req.body._id