0

Here is my a resultant array and now I want to add extra key along with current key. tried .push is not a function

aa[0]['new_key'] = 'blah';    // is not working

My array is looks like.

 var aa =  [
        {
          "main": "56d940bb2b5916181d0906e2",
          "current": 6544
        }
      ]

And my original code is using mongoDB query.

connection.modal.find( { 'user_id' : user_id }, { 'abc.def' : true}, function ( err, result ) {

            if ( !err ) {

                if( result.length ) {

                    result[0]['abc'].def[0]['new_key']  = 'blah';
                    response['success'] = true;
                    response['result'] = result[0]['abc'].def;
                    response['msg'] = 'data fetch';
                    res.json(response);
                } else {

                    response['success'] = false;
                    response['result'] = '';
                    response['msg'] = 'No record found';
                    res.json(response);
                }
            } else {

                response['success'] = false;
                response['result'] = '';
                response['msg'] = 'Error';
                res.json(response);
            }
        });
2
  • What do you mean by "is not working?" Commented Mar 8, 2016 at 12:13
  • btw, there is no push involved. Commented Mar 8, 2016 at 12:16

1 Answer 1

2

I also get same problem a lot i resolved this issue by using this

connection.modal.find( { 'user_id' : user_id }, { 'abc.def' : true}, function ( err, result ) {
 /// ADD THIS LINE /////////
result = JSON.parse(JSON.stringify(result));
 ....
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.