简体   繁体   中英

I am getting Promise <Pending> when using mongoose find() function

I have created a function that gets the list of all products from MongoDB. I am using a mongoose package. I am trying to console log it but instead, I am getting 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:

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM