exports.allUsers = async (req, res, next) => {
try {
let users = await User.find({})
console.log(users)
// [{ role: 'user',
// skills: [...],
// email: 'first.last@mail.com',
// education: [],
// createdAt: 2019-04-02T11:17:33.979Z
// },
// { role: 'admin',
// skills: [...],
// email: 'first.lastname@mail.com',
// education: [],
// createdAt: 2019-04-02T11:17:33.979Z
// } ]
for (let i = 0; i < users.length; i++) {
users[i].bluepages = await bluepagesApi.bluepagesMail(users[i].email)
users[i].image = await bluepagesApi.profileimage(users[i].email)
console.log(users[i].bluepages)
// {
// job: 'Developer',
// givenname: 'Tony',
// ismanager: 'N',
// employeetype: 'P'
// }
// {
// job: 'Job Title',
// givenname: 'Max',
// ismanager: 'N',
// employeetype: 'P'
// }
}
console.log(users)
// [{ role: 'user',
// skills: [...],
// email: 'first.last@mail.com',
// education: [],
// createdAt: 2019-04-02T11:17:33.979Z
// },
// { role: 'admin',
// skills: [...],
// email: 'first.lastname@mail.com',
// education: [],
// createdAt: 2019-04-02T11:17:33.979Z
// } ]
return res.json({
users: users
})
} catch (error) {
next(error)
}
}
如果我在for-loop
内执行console.log(users[i].bluepages)
,则会显示通过API获取的数据,但是如果我在for-loop
中执行console.log(users[i])
,则不会显示新对象。
此外,在我for-loop
之外/之后,这些变化是不可见的。
我也试图用Array.map
来做,但没有任何成功。
来自我的终端的节点.js日志:https://pastebin.com/w7c50YRt
猫鼬会欺骗你。它向文档添加了一个toJSON()
和一个toString()
方法,该方法仅显示模型中定义的属性。如果将其记录到控制台,则会调用toString
,如果将其作为 json 发送,则会调用toJSON()
。
console.log(
users[0], // looks as if bluepages does not exist
users[0].bluepages // but it does actually
);
要让用户表现为对象,请将它们映射到常规对象:
let users = (await User.find({})).map(u => u.toObject());
猫鼬根据用户模式的定义行事。您应该将蓝页和图像键添加到用户的架构中