Express-mongodb中的get方法“传入的参数必须是12个字节的单个字符串或24个十六进制字符的字符串”

时间:2019-05-30 19:41:41

标签: node.js mongodb express

我目前正在使用mongodb-nodejs-express的api实现一个get方法,该方法使用其id在我的数据库中查找所有商店,问题是我在使用get方法时始终收到以下错误:

"error": "Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters"

这是我用来证明的链接

http://localhost:4005/trading/store/1124

这是我所采用的方法

router.get('/store/:id', async (req, res) => {
    const { id } = req.params;
    const db = await connect();
    try{
    const result = await db.collection(collection).find({ store_id: ObjectID(id)});
    res.json(result)
    }
    catch (error) {
        res.status(500).json({ error: error.toString() });
    }
})

这是我的数据库外观的一个例子

{
        "_id": "5cef828a7443855d02fc320e",
        "timestamp": "26/03/2019 18:34",
        "store_id": "1124",
        "user_id": "123",
        "product_id": "949",
        "price": "528"
    },

如果有任何提示或帮助,我将非常感谢,并感谢您抽出宝贵的时间阅读我的问题。

1 个答案:

答案 0 :(得分:1)

您的strore_id只是一个字符串。因此,您的查询应如下所示:

db.collection.find({ store_id: id});
相关问题