我正在成功地从我的Mongo DB中获取一个文档,但是该文档中的所有内容都显示为未定义。下面是一个例子:
...
const user = await UserModel.findById(user_id).exec(); //Getting user normally
console.log(user); // Shows the object normally with the "push_subscriptions" property
console.log(user.push_subscriptions); // Shows undefined
console.log(user.email); //Shows the email normally
...
以下是Console的截图:
下面是用户模式的“push_subscriptions”部分:
...
push_subscriptions: {
type: Array,
},
...
log
正如在Mongoose创建空数组中已经讨论过的那样?
可以使用。lean()方法将用户转换为普通对象:
const user = await UserModel.findById(user_id).exec().lean();