首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Axios createError.js:16未捕获(承诺中)错误:请求失败,状态代码为500

Axios createError.js:16未捕获(承诺中)错误:请求失败,状态代码为500
EN

Stack Overflow用户
提问于 2020-07-01 12:08:37
回答 4查看 5.5K关注 0票数 0

您好,我正在开发一个react-Node Js应用程序,我正在将一个组件类迁移到一个功能组件类,现在我得到了一个问题:createError.js:16 Uncaught (in promise) Error: Request failed with status code 500相同的方法在组件类中工作得很好。下面是我的react组件的代码:

代码语言:javascript
运行
复制
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import { useState } from "react";
import axios from "axios";
import TextField from "@material-ui/core/TextField";
import Button from "@material-ui/core/Button";
import Typography from "@material-ui/core/Typography";

const useStyles = makeStyles((theme) => ({
  root: {
    "& > *": {
      margin: theme.spacing(1),
      width: "25ch",
    },
  },
}));

export default function BasicTextFields(props) {
  const classes = useStyles();
  const [collection, setCollection] = useState("");
  const [field, setField] = useState("");
  const [integrationName, setIntegrationName] = useState("");
  const [total, setTotal] = useState("");

  function handleChange(evt, field) {
    setField(evt.target.value);
    console.log("new value", evt.target.value);
  }

  function handleSubmit(event) {
    console.log("Event delete:" + event);
    var params = new URLSearchParams();
    params.append("collection", collection);
    params.append("field", field);
    params.append("integrationName", integrationName);
    var request = {
      params: params,
    };

    console.log("request: 127.0.0.1:" + request);

    axios.delete(`http://127.0.0.1:8081/firestore/`, request).then((res) => {
      console.log("react1: ", res);
      console.log("react2: ", res.data);
      this.setState({ total: res.data });
    });
  }

  function handleSubmitCount(event) {
    console.log("...count...");
    var params = new URLSearchParams();
    params.append("collection", collection);
    params.append("field", field);
    params.append("integrationName", integrationName);
    var request = {
      params: params,
    };

    console.log("request 127.0.0.1:" + request);
    console.log("BACKEND_HOST:", process.env);
    axios.get(`http://127.0.0.1:8081/firestore/`, request).then((res) => {
      this.setState({ total: res.data });
    });
  }

  return (
    <span>
      <form className={classes.root} noValidate autoComplete="off">
        <TextField
          name="collection"
          onChange={(e) => setCollection(e.target.value)}
          helperText="Collection"
          variant="outlined"
          required
          margin="dense"
        />
        <TextField
          name="field"
          onChange={(e) => setCollection(e.target.value)}
          helperText="Field"
          variant="outlined"
          required
          margin="dense"
        />

        <TextField
          name="value"
          onChange={(e) => setCollection(e.target.value)}
          helperText="Value"
          variant="outlined"
          required
          margin="dense"
        />
        <br />
        <Button
          variant="contained"
          color="primary"
          onClick={(e) => handleSubmit(e)}
          disableElevation
          type="button"
        >
          Delete Registers
        </Button>
        <Button
          variant="contained"
          color="primary"
          onClick={(e) => handleSubmitCount(e)}
          disableElevation
          type="button"
        >
          Count Registers
        </Button>

        <br />
        <br />
        <Typography variant="h6" gutterBottom>
          {total}
        </Typography>
      </form>
    </span>
  );
}

当我单击按钮并使用handleSubmitCount方法时,由于某种原因,它没有调用axios请求,并且抛出了前面提到的问题,我得到了这个错误。

有什么想法吗?

EN

Stack Overflow用户

发布于 2020-07-01 12:35:48

检查Axios文档,您会发现delete方法没有接收到body param,这是您在冒号之后发送的内容。请求只能有一个url参数和一个选项/配置参数(可选)。

https://github.com/axios/axios/blob/master/README.md#axiosdeleteurl-config

我建议你这样做:

代码语言:javascript
运行
复制
axios.delete(`http://127.0.0.1:8081/firestore/${params.toString()}`).then(callback);

由于request只包含您的params,因此不再需要此对象。

请记住,此参数属于查询字符串参数类型,这也是URLSearchParams接口上的用途。

https://developer.mozilla.org/es/docs/Web/API/URLSearchParams

祝好运!

票数 0
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62669544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档