node.js - 更新 mongodb 文档会插入另一个新文档
问题描述
执行后不会更新现有文档,而是插入更新内容的新文档。尝试了很多次通过另一个属性 upsert:false 来 updateOne menthod 但没有工作..
class Product {
constructor(title,img,offerprice,actualprice,description,id){
this.title = title
this.img = img
this.offerprice = offerprice
this.actualprice = actualprice
this.description = description
this._id = id ? new mongodb.ObjectId(id) : null
}
save(){
const db = getDb();
let Opr;
if(this._id){
Opr = db.collection('products').updateOne({ _id:this._id},{
$set:this
})
}
else{
Opr = db.collection('products').insertOne(this)
}
return Opr.then(result => {
console.log(result)
})
.catch(err => console.log(err));
}
解决方案
感谢您的帮助。:) 我在传递错误的 id 作为参数时犯了一个错误.. 现在它工作正常.. https://avrbrands.herokuapp.com
推荐阅读
- swift - SwiftUI TextEditor 禁用编辑但保持可点击
- html - 图标日历在字段上的位置不正确
- python - 无法单击元素,Selenium Python
- .htaccess - 将 http 阿拉伯语 URL 重定向到 https
- php - symfony 5 - 在没有输入类型文件的情况下呈现 FileType 的集合类型
- javascript - 错误:请求失败,状态码为 409
- flutter - Flutter showGeneralDialog 与 Slide 和 Fade 动画
- flexdashboard - Flexdashboard 布局在 4 个方块中设置一个作为选项卡集
- mysql - 如何将一列的列值组合到 MySQL 中的另一列中?
- node.js - 我正在尝试运行 npm install 但它给了我奇怪的错误