【发布时间】:2022-01-10 03:49:31
【问题描述】:
这是我第一次使用 Promise 和 mongoose,我正在尝试将数据保存到全局变量以供以后使用
const getUser = async () => {
let user
try {
user = await clientModel.findOne({username: email})
consoe.log(user)
} catch (e) {
console.log(e)
}
return user
}
const filteredUser = getUser().then((value) => {
return value
}).catch((e) => console.log(e));
console.log(filteredUser)
用户控制台日志显示内容:
{
_id: new ObjectId("61aa75c64e1526131d98f2a1"),
username: 'paul@beatles.uk',
chequing: null,
saving: '1000022',
__v: 0
}
但过滤用户一直显示Promise { <pending> }
【问题讨论】:
-
所有
async函数都返回一个承诺。调用者必须使用await或.then()从该承诺中获取值。有关说明,请参阅Why do I need to await an async function。
标签: javascript mongoose async-await es6-promise