简体   繁体   English

我得到 Promise<pending> 当使用 mongoose find() function</pending>

[英]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.

相关问题 我为什么得到承诺{ <pending> 在Promise.all中使用提取时? - Why am I getting Promise { <pending> } when using fetch in Promise.all? 为什么我会收到 Promise { 待定}? - Why am I getting Promise { Pending }? 尽管在我的控制器中使用了 await,为什么我仍然收到 Promise Pending? - Why am I getting Promise Pending despite using await in my controller? 为什么我在使用带有 Angular 8 的 Pdfmake 时收到“未处理的 promise 拒绝类型错误:null 不是函数”? - Why I am getting "Unhandled promise rejection TypeError: null is not a function" when using Pdfmake with Angular 8? 我正在尝试使用mongoose设置我的mongoDB数据库,但是出现错误“ mongoose默认的Promise库已弃用” - I am trying to setup my mongoDB database using mongoose, but i getting an error “mongoose default promise library is deprecated” 猫鼬设置函数与promise一起使用 - Mongoose set function using with promise Map 对象数组并使用调用 API 的 function 更改一个属性。 我一直在等待 promise - Map array of objects and change one property with a function that calls an API. I keep getting promise pending 得到'承诺{<pending> }' 使用 async-await 执行 tcp 客户端时的消息</pending> - Getting 'Promise { <pending> }' message when using async-await to perform tcp client 为什么我在repl.it上看到此代码中的“Promise {<pending>}”? - Why am I seeing “Promise { <pending> }” from this code on repl.it? 使用noConflict时出现类型错误-“ $不是函数” - I am getting a Type Error with - “$ is not a function” when using noConflict
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM