首页 > 解决方案 > 使用地图功能时的承诺待处理

问题描述

我正在尝试使用该mongoose方法来尝试获取 Profile Schema 下的所有记录find()。代码对我来说看起来不错,当我console.log编写代码块时,它会返回Promise { <pending> }.

我尝试了不同的方法,没有希望。任何帮助,将不胜感激。谢谢,

这是我的代码:

return Profile.find()
    .then(profiles => {
        return profiles.map(profile =>{
            return {
            ...profile._doc
            };
        });
    }).catch(err => {
        //console.log(err);
        throw err;
    })

标签: javascriptnode.jsmongoose

解决方案


那是因为 promise 处于挂起状态。如果您在 promise 解决后需要数据,则必须添加 then 回调。

function getProfiles(){
   return Profile.find()
       .then(profiles => {
          return profiles.map(profile =>{
             return {
                ...profile._doc
             };
          });
       }).catch(err => {
          //console.log(err);
          throw err;
       })
}

getProfiles().then(profiles => console.log('profiles',profiles))

推荐阅读