'ObjectId' 的签名 '(inputId: number): ObjectId' 已弃用,使用静态 createFromTime() 为新的 ObjectId 设置数值

问题描述 投票:0回答:1

我尝试使用 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() 吗?

node.js mongodb objectid
1个回答
0
投票

正如@Joe所指出的,必须进一步检查变量product_id的内容。

如果你使用Mongo shell,请启动它并执行下面的语句。

new ObjectId(<substitute the context of your product_id>)

输出将帮助您了解原因。

© www.soinside.com 2019 - 2024. All rights reserved.