Yes, I have read Mongoose populate after save, but I still can't seem to put the pieces together with promises.
I have tried every different way of using .populate in pushComment() that I can think of, and cannot seem to get it right. Any help is greatly appreciated, Thank you.
endpoint
export function addComment(req, res) {
let newComment = {
author: req.user._id,
body: req.body.comment,
date: new Date()
};
return Deal.findById(req.params.id)
.exec()
.then(handleEntityNotFound(res))
.then(pushComment(newComment))
.then(respondWithResult(res))
.catch(handleError(res));
}
pushComment
function pushComment(comment) {
return function (entity) {
entity.comments.push(comment);
return entity.populate('author').save()
.then(updated => {
return updated
});
};
}
respondWithResult
function respondWithResult(res, statusCode) {
statusCode = statusCode || 200;
return function (entity) {
if (entity) {
res.status(statusCode).json(entity);
}
};
}