[我得到Promise 使用猫鼬find()函数时

问题描述 投票:0回答:1

我创建了一个从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);
    }
}
javascript node.js express mongoose promise
1个回答
1
投票

您需要将异步/等待转移到路由功能:

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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.