listData.forEach(function (row, index) {
console.log(row); --> output JSON format { _id: 5b0bbae16fcc45381a679186, item1: 'item1', item2: 'item2' }
console.log(row.item1); --> output undefined
// If using toJSON() then OK
var json = row.toJSON();
console.log(json.item1); --> output item1
});
listData:使用 mongoose 从 MongoDB 获取数据
但我不明白为什么会这样
[If using toJSON() then OK] although row is JSON format
最佳答案
Mongoose 查询将返回 MongooseDocuments,而不是纯 JavaScript 对象。
尝试使用lean()方法,类似这样:
DataMaster.find({ table: 1 }).sort('-created').lean().exec()
关于javascript - 使用 toJSON() 访问对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50734445/