提问者:小点点

Mongo中对象的数组属性未定义


我正在成功地从我的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,
  },
...

共2个答案

匿名用户

log,然后查看是否存在任何push_subscription键。尝试由于用户是mongoose文档,您可以尝试

正如在Mongoose创建空数组中已经讨论过的那样?

匿名用户

可以使用。lean()方法将用户转换为普通对象:

const user = await UserModel.findById(user_id).exec().lean();