【问题标题】:How to update section in the Mongo collection?如何更新 Mongo 集合中的部分?
【发布时间】:2018-05-18 21:55:20
【问题描述】:

这是我的服务器代码:

router.route('/addComment')
.post(function(req, res) {
  appRequest.update({"_id": req.body._id}, {$push:{comments:req.body.comments}}) // appRequest is my collection
});

我是这样定义我的收藏的:

var locations = new mongoose.Schema({
    title: {
        type: String,
        required: true,
    },
    comments: [{
        identifier: String,
        text: String
    }]
});

我想更新 cmets 并将用户新评论附加到 cmets 数组。

当我尝试调用/addComment URL 时,我在日志中得到以下信息(我将它托管在 Heroku 中):

at=错误代码=H12 desc="请求超时" 方法=POST path="/addComment" 主机=折扣-oman.herokuapp.com request_id=f77cc261-e58a-4eea-a7ad-f74af57972f4 fwd="94.185.30.228" dyno=web.1 连接=0ms 服务=30010ms 状态=503 字节=0 协议=https

我在这里做错了什么?

【问题讨论】:

    标签: node.js mongodb meteor heroku mongoose


    【解决方案1】:

    当请求超时时,您会收到H12 errors。它超时是因为您没有向客户端发送任何响应。例如,可以这样发送响应(如果您的其余代码正确):

    router.route('/addComment').post(function (req, res) {
      appRequest.update(
        { _id: req.body._id },
        { $push: { comments: req.body.comments } },
        {},
        function (err) {
          if (err) {
            return res.status(400).json(error);
          }
    
          return res.status(204).json();
        });
    });
    

    res.json() 用于 JSON 响应。您可以使用res.send() 进行一般回复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多