首页 > 解决方案 > PUT 方法在 Node express 应用程序中仅工作 6 次

问题描述

我使用 Mongodb 作为我的数据库来更新变量的值。我正在使用 node.js 和 express 服务器。

这是我的代码:

router.route("/update/").put( async (req, res) =>{
    const count_update = {
        date: new Date(),
        count: req.body.count
    }
    var searchDate = count_update.date.toISOString().slice(0,10);
    const auth = req.currentUser;
    if (auth) {
    try{
      await User.updateOne(
          { id: auth.uid },
          [{
            $set: {
              count: {
                $cond: [
                  {
                    $gt: [searchDate, { $substr: [{ $last: "$count.date" }, 0, 10] }]
                  },
                  {
                    $concatArrays: [
                      "$count",
                      [{ date: count_update.date, count: count_update.count }]
                    ]
                  },
                  {
                    $map: {
                      input: "$count",
                      in: {
                        $mergeObjects: [
                          "$$this",
                          {
                            $cond: [
                              {
                                $eq: ["$$this.date", { $last: "$count.date" }]
                              },
                              { count: count_update.count },
                              {}
                            ]
                          }
                        ]
                      }
                    }
                  }
                ]
              }
            }
          }]
        )

此代码仅更新变量 6 次。然后我必须重新加载应用程序才能再更新 6 次。

可能是什么问题?

标签: node.jsarraysmongodbmongoosemongodb-query

解决方案


我刚刚发现了问题。这是我的服务器等待响应。浏览器将并发请求限制限制为 6 或其他东西。那就是问题所在。

最好的方法是通过调用结束响应等待:

res.end()

推荐阅读