Unable to add key in object in javascript/node.js [duplicate]

This question already has an answer here:

Previously I believe using (.) dot notation or array key value add a key to an object, but today I am facing a weird issue in which I am not able to add a key/value to an object, let me know what I am doing wrong if any.

Code –

    classes.find(function(err, data){
        if(err) {            
            res.send({"status":500,"message":"Internal Server Error", data: err});
        }
        var k =0;
        for(var i=0; i<data.length; i++) {
            schedule.find({class_id: data[i]._id}, function(err, response){
                if(err)
                    throw err;
                console.log("resp ", response[0].start_date);
                var myObj = data[k];
                //myObj.start_date = response[0].start_date; Tried
                myObj['start_date'] = response[0].start_date;
                console.log("typeof", typeof myObj);
                console.log("myObj._id", myObj._id);
                console.log("::=>", myObj);
                k++;
            })
        }           
    });

Following is the output I am getting, clearly its not adding start_date to myObj.

Result I am getting with the above code –

resp  Sat Jun 06 2015 00:00:00 GMT+0530 (IST)
typeof object
myObj._id 556a12168c9206a6123dd7d1
::=> { _id: 556a12168c9206a6123dd7d1,
  name: 'Electronics',
  credits: 50,
  __v: 0 }
resp  Thu Sep 10 2015 00:00:00 GMT+0530 (IST)
typeof object
myObj._id 556abedc195287dc1136f0be
::=> { _id: 556abedc195287dc1136f0be,
  name: 'Mechanical',
  credits: 60,
  __v: 0 }


Source: javascript

Leave a Reply