Hi, I have this piece of code://comment pagination router.put('/:id/paginate', (req, res) => { //decide by what to sort var sortCriteria = null; switch(req.query.sort){ case 'top': sortCriteria = {'meta.hearts': -1}; break; case 'old': sortCriteria = {date: +1}; break; default: sortCriteria = {date: -1} } var skipDest = parseInt(req.query.page) * 10; Comment.find({quoteId: req.params.id}).sort(sortCriteria).skip(skipDest).limit(10).then((comments) => {userPromises = []; for(var i = 0; i < comments.length; i++){ userPromises.push(User.findOne({_id: comments[i].author})); } Promise.all(userPromises).then((users) => { for(var i = 0; i < comments.length; i++){ comments[i].authorName = users[i].username; console.log(comments) console.log(comments[i].authorName) } console.log(comments) res.json({ comments: comments, user: req.user }) }) }) })So everything works great except the second part of it. What i try doing in the second part of it is to add an additional property to each comment. The console.log() (line 26) logs all the comments however none of them have the authorName property , however console.log(comments[i].authorName) logs the author name as if it would exist. I have totally no idea why this happens, all the code seems correct. Please help!Thanks in advance ;)
Submitted August 26, 2018 at 09:07PM by everek123
No comments:
Post a Comment