2016-03-08 36 views
0

這是我所得到的陣列,現在我想補充與當前密鑰一起額外的鍵。嘗試了 。 推不是一個函數如何沿着添加鍵與數組對象節點JS

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

我的數組是什麼樣子。

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

而我的原始代碼是使用mongoDB查詢。

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); 
      } 
     }); 
+0

解決這個問題你是什麼意思「不工作?」 – Jacob

+0

順便說一句,沒有涉及推動。 –

回答

2

我也得到同樣的問題很多我用這個

connection.modal.find({ 'user_id' : user_id }, { 'abc.def' : true}, function (err, result) { 
/// ADD THIS LINE ///////// 
result = JSON.parse(JSON.stringify(result)); 
.... 
+0

感謝好友(y) – iam

+0

歡迎您,:D –