[英]I am getting Promise <Pending> when using mongoose find() function
I have created a function that gets the list of all products from MongoDB.我创建了一个 function,它从 MongoDB 获取所有产品的列表。 I am using a mongoose package.
我正在使用 mongoose package。 I am trying to console log it but instead, I am getting Promise.
我正在尝试控制台记录它,但我得到的是 Promise。 Here is my code: -
这是我的代码: -
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);
}
}
You need to move async/await to the route function:您需要将 async/await 移动到路由 function:
router.get('/', async function (req,res) {
//Gets all the products being sold by the particular seller
const allProducts = await findAllProducts(userId);
console.log(allProducts);
})
function findAllProducts(sellerId) {
try {
return Products.find( { seller: {
Id: sellerId
}});
} catch (error) {
console.log(e);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.