保存后使用承诺(node.js)填充Mongoose.populate()
是的,我已经读过了,但我似乎仍然无法把承诺与细节结合起来 我已经尝试了各种不同的方法来使用pushComment(),这些方法我都能想到,但似乎无法正确使用。非常感谢您的帮助,谢谢 端点保存后使用承诺(node.js)填充Mongoose.populate(),node.js,mongodb,mongoose,promise,Node.js,Mongodb,Mongoose,Promise,是的,我已经读过了,但我似乎仍然无法把承诺与细节结合起来 我已经尝试了各种不同的方法来使用pushComment(),这些方法我都能想到,但似乎无法正确使用。非常感谢您的帮助,谢谢 端点 export function addComment(req, res) { let newComment = { author: req.user._id, body: req.body.comment, date: new Date() }; return Deal.fi
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));
}
评论
function pushComment(comment) {
return function (entity) {
entity.comments.push(comment);
return entity.populate('author').save()
.then(updated => {
return updated
});
};
}
响应结果
function respondWithResult(res, statusCode) {
statusCode = statusCode || 200;
return function (entity) {
if (entity) {
res.status(statusCode).json(entity);
}
};
}
到底是什么不起作用?如果你将代码剥离到只会给你带来麻烦的部分,那会很有帮助。到底是什么不起作用?如果您将代码剥离到只会给您带来麻烦的部分,这将是很有帮助的。