嗨,我的資料庫中有這樣的條目:資料庫條目并希望從愿望清單中洗掉一個子檔案,例如:222223。我嘗試使用以下代碼這樣做:
async removeProduct(visitorID: number, productID: number) {
const product = await this.visitorModel.findOne({ visitorID: visitorID });
console.log(product);
product.updateOne({
$pull: {
wishlist: productID
}
}, { new: true })
console.log(product);
}
當我嘗試發出洗掉請求時,我的 console.logs 列印了以下結果:
{
_id: new ObjectId("6228c61d0f4a800664f46eb5"),
visitorID: 2122323,
wishlist: [ 222223, 22222, 22222 ],
__v: 2
}
{
_id: new ObjectId("6228c61d0f4a800664f46eb5"),
visitorID: 2122323,
wishlist: [ 222223, 22222, 22222 ],
__v: 2
}
好像它沒有從愿望清單陣列中找到條目“222223”?
uj5u.com熱心網友回復:
什么都沒有發生的原因是因為您沒有告訴貓鼬執行您的請求。
當呼叫findOne(),或任何其他與你的 mongoose 模型相關的方法時updateOne(),deleteMany()你得到的只是一個Query. 如果你想讓它被執行,你需要使用awaitorexec()方法。
這就是你呼叫模型findOne(...)方法時所做的。你只是忘了放一個awaitbefore product.updateOne(...)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/443991.html
