【问题标题】:node.js forEach Array of objects getting undefined value inside the loopnode.js forEach 对象数组在循环内获得未定义的值
【发布时间】:2017-03-24 01:42:38
【问题描述】:

当我遍历对象数组时出现无法解释的“未定义”(这来自具有嵌套文档的 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,但不访问状态和测试属性。我真的需要每个对象的状态。

【问题讨论】:

  • 如果你使用的是mongoose,可以运行u.toObject()获取js对象。

标签: javascript arrays node.js mongodb


【解决方案1】:

天哪,我是个白痴。

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

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

assistantOf: [{ _id:假, id:{类型:字符串}, 状态:{类型:字符串} }],

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 2020-11-21
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    相关资源
    最近更新 更多