[英]How to save a query from model.findOne() on a variable using mongoose
I am trying to save the results from model.findOne() on a variable for using the results on another function.我正在尝试将 model.findOne() 的结果保存在一个变量上,以便在另一个函数上使用结果。 But what I get on the variable is only a pending promise:
但我得到的变量只是一个未决的承诺:
let key = Product.findOne({ name: 'keyboard' }).then(data => { return data });
console.log(key);
and what I see on mongo shell:以及我在 mongo shell 上看到的内容:
Promise { <pending> }
key
is not the result of findOne(), it is a promise. key
不是 findOne() 的结果,它是一个承诺。 The promise has not resolved.承诺尚未解决。
To make your example work, change the code:要使您的示例正常工作,请更改代码:
let key = await Product.findOne({ name: 'keyboard' }).exec();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.