【问题标题】:Update query timing out after 10 seconds despite successfully submitting in React app尽管在 React 应用程序中成功提交,但更新查询在 10 秒后超时
【发布时间】:2020-11-24 03:57:55
【问题描述】:

我有以下架构:

const SubmitDebtSchema = new Schema ({
  balance: [{
    balanceDate: Date,
    newBalance: Number
  }]
});

我有以下函数将新对象附加到数组中:

  module.exports = async (req, res) => {

    let newDateObject = {
      balanceDate: req.body.balanceDate,
      newBalance: req.body.newBalance
    };

    await SubmitDebt.findOneAndUpdate(
      { _id: req.query.id },
      { $push: { balance: newDateObject } },
      {new: true, useFindAndModify: false}
    );

  };

但是,尽管数据库成功更新,但它在 10 秒后超时并显示以下错误消息:

2020-11-23T16:51:57.138Z 25fa69c2-91a1-4a74-8034-af132d4d8eb3 任务 10.01 秒后超时

有人对如何解决这个问题有任何反馈吗?成功提交后,它也不会推送到我的“仪表板”。

这是我的 Axios 前端调用:

onSubmit = async (e) => {

      e.preventDefault();

      let newBalanceDate = new Date();

      this.calculateUpdatedBalance()

      await axios.post("/api/edit/editDebtBalance",
      {
          balanceDate: newBalanceDate,
          newBalance: this.calculateUpdatedBalance(),
    },
    {
      params: {
        id: this.props.match.params.id
      }
    }
  )

      this.props.history.push('/dashboard');
    }

编辑:我的完整无服务器函数文件:

const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const SubmitDebt = require("../submit/submitDebtSchema");

require("dotenv").config();

const app = express();

app.use(bodyParser.urlencoded({
  extended: true
}));

mongoose.connect(process.env.MONGO_URI);


  module.exports = async (req, res) => {

    let newDateObject = {
      balanceDate: req.body.balanceDate,
      newBalance: req.body.newBalance
    };

    return await SubmitDebt.findOneAndUpdate(
      { _id: req.query.id },
      { $push: { balance: newDateObject } },
      {new: true, useFindAndModify: false}
    );

  };

【问题讨论】:

    标签: reactjs mongodb mongoose


    【解决方案1】:

    您尝试返回值如何? lambda 函数仅在您返回某些内容时才会停止,试试这个。

    return await SubmitDebt.findOneAndUpdate(
          { _id: req.query.id },
          { $push: { balance: newDateObject } },
          {new: true, useFindAndModify: false}
        );
    

    请注意,我只是在 await 语句之前添加了 return,就是这样。

    编辑:啊……我认为这应该可行

    let response = await SubmitDebt.findOneAndUpdate(
          { _id: req.query.id },
          { $push: { balance: newDateObject } },
          {new: true, useFindAndModify: false}
        );
    return res.send(response)
    

    因为您使用的是快递应用。很抱歉我之前忽略了它。

    【讨论】:

    • 刚试过这个,不幸的是没用!它仍然超时。我认为它需要一个回调,但不知道如何构建它。
    • 我现在已经添加了 - 可以肯定它不会有帮助。
    • 顺便说一句,您使用的是哪个节点版本?
    • 我相信是 v12.16.3
    • 那么你肯定不需要回调,这应该有效,也许我错过了什么。
    猜你喜欢
    • 1970-01-01
    • 2016-04-02
    • 2019-05-03
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    • 2020-11-07
    相关资源
    最近更新 更多