我尝试使用 ObjectId 获取单个产品,但无论我在何处编写代码:
const { ObjectId } = require('mongodb');
static findById(product_id)
{
// console.log("Collected product id:",product_id);
//db is the database
return db.collection('Product_data').find({"_id":new ObjectId(product_id)}).next()
.then(singleData=>{
return singleData;
})
.catch(err=>{
console.log("Product not found",err)
})
}
尽管我得到了结果,但它在 ObjectId 上划掉了。任何人都可以帮助使用 createFromTime() 吗?
正如@Joe所指出的,必须进一步检查变量product_id的内容。
如果你使用Mongo shell,请启动它并执行下面的语句。
new ObjectId(<substitute the context of your product_id>)
输出将帮助您了解原因。