I have an object array return from mongoose find method. However it is in below format:
var value = [{'value': {'year': 2018, 'month':3, 'count': 123}}];
How can I convert it to below:
var value = [{'year': 2018, 'month':3, 'count': 123}];
I tried below to convert but still print out the original format.
var result = [];
result.push(value);
var arr = [];
Object.keys(result).forEach(function(item) {
arr.push(result[item]);
console.log(result[item].year); //undefined
console.log(result[item]['year']; //undefined
})
console.log(arr);
Below is the retrieving method.
function findStats(result, cb) {
monthly_order.find({})
.select('-_id')
.sort({
"_id.year": 1,
"_id.month": 1
})
.exec(function (err, result) {
if (err) {
console.log('Error find: ', err);
return cb(err);
}
if(!result){
return cb({'message':'No Order.'});
}else{
console.log(result);
return cb(null, result);
//var arr = result.map(x => x.value);
//console.log(arr);
//return cb(null, arr);
}
});
}



resultthat you pass tofindStatsgoes completely unused, as it's redefined by.exec. Are you sure thatresult, inside the.exec(function(err, result) { ... }), is what you think it is?