我在一个模型中使用了一个findOne,它返回一个响应。
const findAccount = await this.accountModel.findOne({
_id: ObjectId(param.accountId),
});
console.log(findAccount);
//The above logs the data
console.log(findAccount.role, findAccount._id);
//As for this, findAccount.role is undefined, but the findAccount._id was logged.
可能的原因是什么?
发布于 2021-06-20 14:53:15
findOne
方法返回一个Mongoose文档的实例,这可能会导致这样的情况。要获得Javascript纯文本对象,可以使用lean()
method。试试这个:
const findAccount = await this.accountModel.findOne({
_id: ObjectId(param.accountId),
}).lean();
console.log(findAccount);
//The above logs the data
console.log(findAccount.role, findAccount._id);
https://stackoverflow.com/questions/68056844
复制相似问题