简体   繁体   English

node.js forEach 对象数组在循环内获得未定义的值

[英]node.js forEach Array of objects getting undefined value inside the loop

Getting unexplained "undefined" when I loop through an array of objects (this is coming from mongo document that has a nested document)当我遍历对象数组时得到无法解释的“未定义”(这来自具有嵌套文档的 mongo 文档)

user.assistantOf.forEach(function(u){   
    console.log(u);
    console.log(u.id +' '+ u.test + ' ' + u.status);

        arr.push(u.id);
})

{ test: 'test1',                                                                                           
  status: 'active',                                                                                        
  id: '58aa50fb6b364b150405a183' }  

58aa50fb6b364b150405a183 undefined undefined  

{ test: 'test2',                                                                                           
  status: 'active',                                                                                        
  id: '58cff0e0c5aa2b0011ddbf3e' }    

58cff0e0c5aa2b0011ddbf3e undefined undefined  

So, the loop can access object properties, so it seems - it prints id, but does not access status and test properties.所以,循环可以访问对象属性,看起来 - 它打印 id,但不访问状态和测试属性。 I really need the status of each object.我真的需要每个对象的状态。

OMG, I'm an idiot. OMG,我是个白痴。

I did not have "status" in my mongoose model of this array.我在这个数组的猫鼬模型中没有“状态”。

So, here's how it should be, and it works now:所以,它应该是这样的,它现在可以工作:

assistantOf: [{ _id:false, id: {type: String}, status: {type: String} }], AssistantOf: [{ _id:false, id: {type: String}, status: {type: String} }],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM