【发布时间】:2020-02-28 12:28:23
【问题描述】:
我创建了一个从 MongoDB 获取所有产品列表的函数。我正在使用猫鼬包。我正在尝试控制台记录它,但相反,我得到了 Promise 。这是我的代码:-
router.get('/', function (req,res) {
//Gets all the products being sold by the particular seller
const allProducts = findAllProducts(userId);
console.log(allProducts);
})
async function findAllProducts(sellerId) {
try {
let products = await Products.find( { seller: {
Id: sellerId
}});
return products;
} catch (error) {
console.log(e);
}
}
【问题讨论】:
-
由于
findAllProducts是async function,它返回一个承诺。你也必须await。
标签: javascript node.js express mongoose promise