首页 > 解决方案 > 为什么这个结果会发生两次?相隔几分钟

问题描述

好消息是,一切都按预期工作。除了,结果发生了两次,我不知道为什么?我已经搜索了代码,没有看到addDocsList被调用了两次。

这是调用相关 API 路由的方法。它所做的只是调用一个向 mySql 表添加几行的路由:

addDocsList() {
      fetch(
        API_URL + `/hca/documents/add/${this.state.lastHCA_Id}`,
        {
          method: "PUT",
          body: JSON.stringify({
            lastHCA_Id: this.state.lastHCA_Id
          }),
          headers: { "Content-Type": "application/json" },
        }
      ).then((res) => {
        if (!res.ok) {
          throw new Error();
        }
        return res.json();
      })
        .then((data) => console.log(data))
      .catch((err) => console.log(err))
      
    }

它调用这条路线。该路由确实按计划将行添加到表中,大约一分钟后,它会再次执行此操作。但是,无论何时,路由末尾的控制台日志都不会记录。这是否意味着由于某种原因它实际上并没有完成和循环?

app.put("/hca/documents/add/:lastHCA_Id", function (req, res) {
  console.log("It is getting to the add docs route.");
  const hcaId = req.body.lastHCA_Id;
  console.log(`HCA ID: ${hcaId}`);
  connection.getConnection(function (err, connection) {
    connection.query(
      `INSERT INTO hca_docs (hca_id, doc, section)
        VALUES
      (${hcaId}, 'Personnel Files', 'Location'),
      (${hcaId}, 'I-9', 'Location'),
      (${hcaId}, 'Time Keeping System', 'Location'),
      (${hcaId}, 'Posting Requirements', 'Location'),
      (${hcaId}, 'Employee Data Spreadsheet', 'Payroll'),
      (${hcaId}, 'Employee Surveys', 'Other')
      `
    ),
    function (error, results, fields) {
        connection.release();
        if (error) throw error;
        res.json(results);
        console.log("Docs inserted into Table"); <--- This log never happens. 
      }
  });
});

任何帮助表示赞赏。

标签: mysqlreactjsrest

解决方案


推荐阅读