【发布时间】:2021-11-19 02:00:49
【问题描述】:
目标:前端 react 应用可以将数据作为 POST 请求发送到 Heroku 后端。
框架:使用 Axios 进行反应
我一直在尝试在 Axios 发布请求功能上设置断点,但它似乎不起作用。我已经用.then 设置了一个承诺声明,但是没有任何响应被发送回控制台。
下面是提交时正在运行的 React 组件和 Axios POST。
我们将不胜感激任何有关此事的帮助。
我已经测试了 POSTMAN 的 POST 请求,并且数据的状态为 200 并且确实已发送。
另外,我更新了 promise 和 try-catch。 Promise 被注释掉了,因为我无法让它使用当前的语法。如果您觉得最好使用,请查看代码并了解语法问题的原因。
import React from 'react';
import {useState} from 'react';
import Axios from 'axios'
//import { response } from 'express';
const QuoteForm = () => {
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [question, setQuestion] = useState("");
/* This is the promise version however it has sytax issues :
the funtion containing the promise doesn't properly close so return expects } instead has )
const custData = { name:name ,
email:email ,
question:question} ;
const submitQuestion = new Promise ((resolve, reject)=>
resolve(Axios.post('https://hookahsite-backend.herokuapp.com || https://localhost:8000 ' , custData)
.then(()=> {console.log("success, data sent")})
.catch(()=>{console.log("error when sending")})
)
*/
//this uses try catch however the backend is not getting hit with any data
//tested this same request in Postman and it works
function submitQuestion() {
try {
Axios.post('https://hookahsite-backend.herokuapp.com ' ,
{
name:name ,
email:email ,
question:question
},
)
}
catch (err) {console.error(err);}
}
return(
<React.Fragment>
<form id="quoteForm"
>
<h1 id="quoteTitle">Quote Help Form</h1>
<p id="quotePar">Please provide your Name, Contact Email, and what products you would like more information about in this form :</p>
<label id="formName" className="Form">
Name:
<input type="text" name="name"
onChange={(event) => { setName(event.target.value);}}
/>
</label>
<label id="formEmail" className="Form">
Email:
<input type="text" name="email"
onChange={(event) => { setEmail(event.target.value);
}}/>
</label>
<br/>
<label id="formQuestion" className="Form" >
What products would you like to know more about:
<input type="text" name="help"
onChange={(event) => { setQuestion(event.target.value);
}}/>
</label>
<br/>
<br/>
<button id="quoteSubmit" type="submit"
onClick =
{
submitQuestion
}
/*
old way
{()=>
submitQuestion()
}
*/
>Submit </button>
</form>
›
</React.Fragment>
)
};
export default QuoteForm;
服务器端代码:
请注意,前端的 Try Catch 不会收到要抛出的错误。后端代码也应该发送控制台日志,而不是。现在,如果我通过邮递员发送此内容,则会有一个控制台日志,并且状态为 200
app.post('/' , (req,res) => {
const {email, name, question} = req.body;
res.header("Access-Control-Allow-Origin", "*");
console.log(`Your Email is ${email} and your name is ${name} and your ${question}`);
//MYSQL updating table
pool.query("INSERT INTO customer_questions (name, email, question) VALUES (?,?,?)",
[name, email, question], (err,res)=> {
if (err) {
console.log(err)
}else {
res.send('data sent')
}
}
);
});
【问题讨论】:
-
您不应该将 try/catch 与 .then() 混合使用,坚持一种方法。使用 async/await 尝试/捕获或使用 .then 和 .catch
-
同意,
try/catch语法通常与async/await一起使用,而不是 Promise 链,因为 Promise 链有自己的.catch处理程序模式。 -
只更新了 try/catch 。
-
对于那些正在审查的人。我已经更新了一个注释掉的承诺。如果您认为最好使用,请告知如何更正我已注释掉的版本。
-
@CristoferVillegas 我更新了代码并注释掉了 Promise 方法。对于更新后的代码,您还有什么其他想法吗?