【发布时间】:2020-09-08 17:41:33
【问题描述】:
所以基本上我想遍历文档并使用 .find 方法检查是否有符合条件的文档,如下所示:
//example model
const example = new ExampleModel ({
exampleName : "randomName",
exampleValue : 0
})
const doc = ExampleModel.findOne( {name : "randomName"} )
if (doc) console.log("There is a Document with that name!")
问题在于它不起作用,当我执行console.log(doc) 时,它会记录一个查询,但我想要的是文档而不是查询。
提前致谢!
【问题讨论】:
-
试试
((await ExampleModel.findOne({name: "randomName" }).exec()) !== null) -
你需要 chaning 然后像这样赶上尝试
doc.then(result => console.log(result)).catch(error => console.log(error))
标签: javascript node.js mongodb mongoose