0
function getAllExperiences(){
    return new Promise(function(resolve,reject){
        Experiences.find({status:'live'},function(err,results){
            if(err){
                reject(err);
            }
            else
            {   
                var finalResult = [];
                results.forEach(function(objs){
                    objs.convertedPrice = 'aaaaaaaaaa';
                    finalResult.push(objs);
                });
                console.log(finalResult[0].convertedPrice)
                console.log(finalResult)
                resolve(finalResult);
            }
        });
    });
}

i need to add element to every json object into foreach loop but not showing into console.log(finalResult). but when i console.log(data[0].convertedPrice) it display convertedPrice

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • If it's `Mongodb` try `.find({..}).toArray(function(err, result){...})` – Daniel Krom May 08 '18 at 08:14
  • In short add `.lean()` to your query to get plain objects you can modify. Also, mongoose methods return promises already. So `return Experiences.find(...).lean().then( results => { /* rest of code */` instead. No need to wrap a callback with a Promise manually – Neil Lunn May 08 '18 at 08:24
  • @NeilLunn thanks it works for me. – lokesh jain May 08 '18 at 08:30

0 Answers0