繁体   English   中英

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

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

当我遍历对象数组时得到无法解释的“未定义”(这来自具有嵌套文档的 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  

所以,循环可以访问对象属性,看起来 - 它打印 id,但不访问状态和测试属性。 我真的需要每个对象的状态。

OMG,我是个白痴。

我在这个数组的猫鼬模型中没有“状态”。

所以,它应该是这样的,它现在可以工作:

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

暂无
暂无

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

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