0
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: have get Data from MongoDB with mongoose

But I do not understand why so

[If using toJSON() then OK] although row is JSON format

Tính Ngô Quang
  • 4,400
  • 1
  • 33
  • 33

1 Answers1

1

Mongoose queries will return MongooseDocuments and not the plain JavaScript objects.

Try using the lean() method, something like this:

DataMaster.find({ table: 1 }).sort('-created').lean().exec()

Jalay
  • 66
  • 1
  • 7