javascript - 来自 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
...
这是控制台的屏幕截图:
这是用户模式的“push_subscriptions”部分:
...
push_subscriptions: {
type: Array,
},
...
解决方案
登录user.toJSON()
并查看是否存在任何 push_subscription 密钥。尝试user.toJSON().push_subscriptions
作为用户是猫鼬文档,您可以尝试toJSON
或toObject
推荐阅读
- c# - 肥皂请求的授权
- excel - 如何将定义的范围复制到 Excel for Mac 中的公式中?
- c++ - c++ 11 中的 if else 编译时间
- c# - 什么是 C# 对象模式匹配?
- window - Tcl/Tk:获取调整后窗口的默认位置(和/或是否使用默认位置)
- typescript - Exceljs 应用程序“正在处理您的功能”消息
- javascript - 我在 Angular 项目中有多个 ngFor 使用 trackBy:trackByFunction 和单个函数还是创建多个函数?
- flutter - 名称 appstate 不是类型,因此不能用作类型参数
- php - 在 ACF Pro 中根据选择值动态创建字段 | WordPress,WooCommerce
- python - PyCharm/IntelliJ 中的红色下划线(未解决的引用)停止工作 - 在我的一些 Anaconda 项目中