Below function returns an array in 'results' argument:
module.exports.get = function (req, res) {
objModel.find(function (err, results) {
res.json({ record: results })
})
};
I want to add its reference collection record list in one new object for each element. Just like the following:
for (var i = 0; i < results.length; i++) {
module.exports.get = function (req, res) {
objModel.find({ _id: results[i]._id }, function (err, record) {
results[i]["objNew"] = record
})
};
}
My full code looks like:
module.exports.get = function (req, res) {
objModel.find(function (err, results) {
for (var i = 0; i < results.length; i++) {
module.exports.get = function (req, res) {
objModel.find({ _id: results[i]._id }, function (err, record) {
results[i]["objNew"] = record
})
};
}
res.json({ recordList: results })
})
};
It return error: "objNew" is unknown.
I display output record json list in this link: Plunker
objModel.find(...)) you are bound to get incorrect results.