javascript - 使用地图功能时的承诺待处理
问题描述
我正在尝试使用该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;
})
解决方案
那是因为 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))
推荐阅读
- django - Mac 上的 Django 安装
- angular7 - 如何延迟 Angular 7 中错误的 http 响应
- sql-server - SQL Server 中公式的动态计算
- go - 并行运行基准测试,即模拟同时请求
- node.js - Node Express Gateway 多个 API 端点
- verilog - 为什么一旦测试台进入 fr_count 条件块,我的“数据”寄存器变量会重置为 0?
- python - Openpyxl 没有写入我的 Excel 电子表格
- scala - 在这种情况下我可以避免变异吗?
- php - 2 如何改变'X' col。如果表格的每一行表示在单独的 HTML 表单中,并且每个提交按钮更改“X”值,则单击一次表格的值?
- android - Android Sqlite onupgrade 从数据库中删除表