首页 > 解决方案 > 502 Bad Gateway 和 503 Service Unavailable - 但不一致

问题描述

我有这个代码,一旦你输入了一个人的名字和职位,它就会遍历一个不同的表列表并记录它们,其中它将保存一些以后表单中的数据。问题是它只会创建一些记录,而其他记录会给我一个 502 或 503 错误。而且是不一致的。它可能会按预期创建 3 或 4 条记录,但每次都会创建不同的 3 或 4 条。502 和 503 错误每次发生在不同的调用上。所以我知道代码本身确实有效,实际上它在我的本地构建中完美运行。但是在我的生产版本中,会发生这种情况。

我确信有一种更有效的方法可以做到这一点,所以我也很高兴听到这方面的想法。我有多个表的唯一原因是我们的表单上有大约 200 个字段,而 mySql 不允许我将它们放在一起。

这是代码:

createQuestions() {
    fetch(
      API_URL + `/interview/create/questions/${this.state.lastEmployeeId}`,
      {
        method: "PUT",
        body: JSON.stringify({
          lastEmployeeId: this.state.lastEmployeeId,
          table: "audit_general",
        }),
        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))
      .then(
        fetch(
          API_URL + `/interview/create/questions/${this.state.lastEmployeeId}`,
          {
            method: "PUT",
            body: JSON.stringify({
              lastEmployeeId: this.state.lastEmployeeId,
              table: "audit_culture",
            }),
            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))
      )
      .then(
        fetch(
          API_URL + `/interview/create/questions/${this.state.lastEmployeeId}`,
          {
            method: "PUT",
            body: JSON.stringify({
              lastEmployeeId: this.state.lastEmployeeId,
              table: "audit_performance",
            }),
            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))
      )
      .then(
        fetch(
          API_URL + `/interview/create/questions/${this.state.lastEmployeeId}`,
          {
            method: "PUT",
            body: JSON.stringify({
              lastEmployeeId: this.state.lastEmployeeId,
              table: "audit_policies",
            }),
            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))
      )
      .then(
        fetch(
          API_URL + `/interview/create/questions/${this.state.lastEmployeeId}`,
          {
            method: "PUT",
            body: JSON.stringify({
              lastEmployeeId: this.state.lastEmployeeId,
              table: "audit_risk",
            }),
            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))
      )
      .then(
        fetch(
          API_URL + `/interview/create/questions/${this.state.lastEmployeeId}`,
          {
            method: "PUT",
            body: JSON.stringify({
              lastEmployeeId: this.state.lastEmployeeId,
              table: "audit_strategy",
            }),
            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))
      )
      .then(
        fetch(
          API_URL + `/interview/create/questions/${this.state.lastEmployeeId}`,
          {
            method: "PUT",
            body: JSON.stringify({
              lastEmployeeId: this.state.lastEmployeeId,
              table: "audit_rewards",
            }),
            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))
      )
      .then(
        fetch(
          API_URL + `/interview/create/questions/${this.state.lastEmployeeId}`,
          {
            method: "PUT",
            body: JSON.stringify({
              lastEmployeeId: this.state.lastEmployeeId,
              table: "audit_workforce",
            }),
            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))
      );
  }
}

我得到的错误是:

List.js:401 PUT https://ppr-team.com/api/interview/create/questions/41 502 (Bad Gateway)

List.js:443 PUT https://ppr-team.com/api/interview/create/questions/41 502 (Bad Gateway)

List.js:422 PUT https://ppr-team.com/api/interview/create/questions/41 502 (Bad Gateway)

List.js:464 PUT https://ppr-team.com/api/interview/create/questions/41 503 (Service Unavailable)

List.js:485 PUT https://ppr-team.com/api/interview/create/questions/41 503 (Service Unavailable)

我不确定在哪里可以找到它。

标签: javascriptmysqlreactjsapi

解决方案


服务器是卑鄙的,您应该检查它是否运行正确且配置正确。真的没什么,500 范围意味着服务器做错了什么,或者是一个不使用正确状态代码的坏 api。


推荐阅读