exports.allUsers = async (req, res, next) => {
try {
let users = await User.find({})
console.log(users)
// [{ role: 'user',
// skills: [...],
// email: '[email protected]',
// education: [],
// createdAt: 2019-04-02T11:17:33.979Z
// },
// { role: 'admin',
// skills: [...],
// email: '[email protected]',
// 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: '[email protected]',
// education: [],
// createdAt: 2019-04-02T11:17:33.979Z
// },
// { role: 'admin',
// skills: [...],
// email: '[email protected]',
// education: [],
// createdAt: 2019-04-02T11:17:33.979Z
// } ]
return res.json({
users: users
})
} catch (error) {
next(error)
}
}
If I do a console.log(users[i].bluepages) inside my for-loopthe data that is fetched via an API is shown but if I do a console.log(users[i]) in my for-loop the new object is not shown.
Also outside/after my for-loop the changes are not visible.
I also tried to do it with Array.map without any success.
Node.js logs from my terminal: https://pastebin.com/w7c50YRt
for-loopexecutes correctly and the json is sent when the loop has finished.