gpt4 book ai didi

javascript - collection.find() 无法按预期工作 - Node.js 和 MongoDB

转载 作者:太空宇宙 更新时间:2023-11-04 02:03:15 24 4
gpt4 key购买 nike

下面的代码应该打印用户 friend 的姓名:

collection.findOne({name: "jason"},
function(err, document) {

collection.find({ id: { "$in": document.friends}}).then(function(err, doc)
{
console.log(doc.name);

if(err) {
return console.log(err);
}

})

});

我 100% 确定该文档不是未定义的。我可以记录文档的信息,但它不会打印 friend 的任何信息。也没有错误。它只是等待。你能告诉我问题出在哪里吗?谢谢。

编辑:我的用户看起来像这样:

{
"id": 22448856945,
"id_str": "224488456945",
"name": "jason",
"friends" : [954962638965, 3212489563163, 1659795263231]
}

最佳答案

Collection.find 将返回结果数组。尝试console.log(doc[0].name)。您必须循环遍历结果。

关于javascript - collection.find() 无法按预期工作 - Node.js 和 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45207855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com