您好,我正在尝试为我的 SPA 添加更新功能,但似乎遇到了这个问题
blogsRouter.put('/:id', (request, response) => {
const body = request.body
const blog = Blog ({
title: body.title,
author: body.author,
url: body.url,
likes: body.likes,
userId: body.userId,
userName: body.userName
})
Blog.findByIdAndUpdate(request.params.id, blog)
.then(updatedBlog => {
response.json(updatedBlog.toJSON())
})
.catch(error => console.log(error))
})
Run Code Online (Sandbox Code Playgroud)
它捕获了这个错误
Performing an update on the path '_id' would modify the immutable field '_id'
Run Code Online (Sandbox Code Playgroud)
我不确定这里发生了什么,因为据我了解,我并没有尝试更新 _field ,如果我的方法试图自动执行此操作,那么更好的方法是什么?