Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js expressmongodb中的get方法;传入的参数必须是12字节的单个字符串或24个十六进制字符的字符串;_Node.js_Mongodb_Express - Fatal编程技术网

Node.js expressmongodb中的get方法;传入的参数必须是12字节的单个字符串或24个十六进制字符的字符串;

Node.js expressmongodb中的get方法;传入的参数必须是12字节的单个字符串或24个十六进制字符的字符串;,node.js,mongodb,express,Node.js,Mongodb,Express,我目前正在使用一个api,mongodb nodejs express实现了一个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 这

我目前正在使用一个api,mongodb nodejs express实现了一个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"
    },

我非常感谢任何提示或帮助,并提前感谢您抽出时间阅读我的问题。

您的strore\u id只是一个字符串。因此,您的查询应该如下所示:

db.collection.find({ store_id: id});

@dimitristseggenes感谢您的建议,这是经过修改的消息
{“error”:“TypeError:将循环结构转换为JSON”}
console.log(result)并在此发布输出感谢您抽出时间回答,我收到了
“错误”:“TypeError:db.collection.find不是函数”
,没有使用
控制台.log(result)
命令的输出。它对您有效吗<代码>常量结果=等待db.collection(collection.find()