axios React js -在Post方法之后产生动作

wribegjk  于 2022-11-23  发布在  iOS
关注(0)|答案(1)|浏览(146)

我正在用Axios、ExpressJS和MySQL开发一个React应用程序。我想在将数据发送到数据库后重定向用户。目前,数据库上的数据是正确的,但重定向、确认吐司和电子邮件发送都没有发生。console.log(response);返回空,如何触发动作?
我试着不输入.then()响应,但没有任何改变

import React, { useState } from "react";
import styled from "styled-components";
import axios from "axios";
import { Toast } from "bootstrap";
import {useNavigate } from "react-router-dom";

const client = axios.create({
  baseURL: "http://localhost:9999",
});

export default function AddStructure() {
  const [assetReg, setAssetReg] = useState("");
  const [structureNameReg, setStructureNameReg] = useState("");
  const [emailReg, setEmailReg] = useState("");
  const [townReg, setTownReg] = useState("");
  const [informationReg, setInformationReg] = useState("");
  const [numberStructuresReg, setNumberStructuresReg] = useState("");
  const [newsletterReg, setNewsletterReg] = useState("");
  const [manageScheduleReg, setManageScheduleReg] = useState("");
  const [flyerReg, setFlyerReg] = useState("");
  const [drinksReg, setDrinksReg] = useState("");
  const navigate = useNavigate();

  const config = {
    SecureToken: "39ced67b-1fcc-48a2-8d18-48e5d1dc9ad8",
    To: "sport.admin@yopmail.com",
    From: emailReg,
    Subject: "confirmation creation of your structure",
    Body: `Dear partner, we are pleased to confirm the creation of your structure. ${structureNameReg}  glass.`,
  };

  const AddStructure = (e) => {
    e.preventDefault();
    if (!assetReg || !structureNameReg || !emailReg || !townReg) {
      Toast.error(
        "please provide value into asset, structure name , email and town"
      );
    } else {
      // eslint-disable-next-line no-unused-expressions
      client
        .post("/addStructure", {
          asset: assetReg,
          structure_name: structureNameReg,
          email: emailReg,
          town: townReg,
          information: informationReg,
          number_structure: numberStructuresReg,
          newsletter: newsletterReg,
          manage_schedule: manageScheduleReg,
          flyer: flyerReg,
          drinks: drinksReg,
        })
        .then((response) => {
          console.log(response);
          Toast.success("Structure added Successfully");
          if (window.email) {
            window.email.send(config);
          }
          setTimeout(() => navigate("/"), 500);
        })
        .then.catch((err) => Toast.error(err.response.data));
    }
  };
nx7onnlm

nx7onnlm1#

在您的代码中,AddStructure组件中没有函数调用,只是在组件返回中没有dom元素。

相关问题