Hi campers,
What I am trying to do:
Get all forum threads (an array of objects), then for each thread get the user associated with it and assign the user’s name to the thread. The thread objects have a user ID based on what user created the thread. The user objects (found separately) have user names. I want to do this because if the user changes their name, all threads will reflect that change.
The problem is that the response is sending the threads before they are updated with the user name. I think it is because the findById promises are not resolving before the response triggers. I tried wrapping it in my own self-made promise, but it still has the issue.
Do you know any way to make it wait for the User.findById promises to complete before sending?
// @desc Get all threads in section
// @route GET api/:forumSection/
// @access PUBLIC
router.get("/:forumSection", (req, res) => {
Thread.find({ forumSection: req.params.forumSection })
.sort({ date: -1 })
.then(threads => {
return new Promise((resolve, reject) => {
for (thread of threads) {
User.findById(thread.user).then(user => {
thread.userName = user.name;
console.log(thread.title);
});
}
resolve();
}).then(res.json(threads);
})
.catch(err => res.status(404).json({ nothreadfound: "No thread found" }));
});