javascript - 当我尝试访问我查询的文档元素时,Mongoose 返回 undefined
问题描述
上下文:我试图简单地控制台记录我查询的文档中的值。我成功地找到了文件。我知道是因为我在打印出我正在寻找的文档的价值之前打印出文档。文档打印查找,另一方面,该字段显示为未定义。
这是代码:
架构:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let TextSchema = new Schema({
text_ID: {type: Number, required: true},
title: {type: String, required: true, max: 100},
body: {type: String, required: true},
});
// Export the model
module.exports = mongoose.model('texts', TextSchema);
我知道 Mongo 会为所有东西创建自己的 ID,但现在请忍受我自己的 ID。这是有问题的代码:
textModel.find({text_ID: currentTextLocation}, function(err, doc){
if(err){
console.log('error: ' + err);
}
console.log(doc);
console.log(doc.body);
currentTextBody = doc.body;
});
这是它给我的输出:
Database connection established!
[
{
_id: 5dc2915f1c9d440000ed7077,
text_ID: 1,
title: 'Kevin Abstract',
body: 'Kevin Abstract was a famous american rap artist.'
}
]
undefined
最后是它的样子:
Database connection established!
[
{
_id: 5dc2915f1c9d440000ed7077,
text_ID: 1,
title: 'Kevin Abstract',
body: 'Kevin Abstract was a famous american rap artist.'
}
]
Kevin Abstract was a famous american rap artist.
我已确保架构与正确的集合相关。我尝试打印返回对象的文档类型。
解决方案
推荐阅读
- google-bigquery - 存储快速增长的数据量的最佳设计/技术
- sql - 使用触发器根据 postgresql 中另一列的年份更新列
- python - 在views.py中保存更改时,作业设置表单重置活动/非活动属性
- c++ - 错误:移动承诺时“尝试使用已删除的功能”
- javascript - HTML和SVG如何通过鼠标单击仅移动一条线的一个端点?
- dart - 如何在 Flutter 中绘制自定义圆角矩形边框(ShapeBorder)?
- return - 类型日期返回内容
- python - Python setuptools将脚本安装到路径
- ios - 关闭和打开我的应用程序直接进入主页,无需验证用户电子邮件
- typescript - 打字稿接口:指定完整的特定枚举作为参数