Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
保存后使用承诺(node.js)填充Mongoose.populate()_Node.js_Mongodb_Mongoose_Promise - Fatal编程技术网

保存后使用承诺(node.js)填充Mongoose.populate()

保存后使用承诺(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

是的,我已经读过了,但我似乎仍然无法把承诺与细节结合起来

我已经尝试了各种不同的方法来使用pushComment(),这些方法我都能想到,但似乎无法正确使用。非常感谢您的帮助,谢谢

端点

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);
    }
  };
}

到底是什么不起作用?如果你将代码剥离到只会给你带来麻烦的部分,那会很有帮助。到底是什么不起作用?如果您将代码剥离到只会给您带来麻烦的部分,这将是很有帮助的。