【发布时间】:2020-06-03 09:31:51
【问题描述】:
我正在使用 ReactJs 和 NodeJS 将数据发送到 MySql,在我的 frontPage.js 中问题是我已经测试了使用 Postman 发布数据的 API,并且没有出现错误。当我尝试使用 ReactJs 时,问题就来了,我编写了这个代码:
class InputEndUser extends Component {
constructor(props){
super(props);
this.state = {
project_type: '',
status : '',
subject : '',
enduser_name : '',
location : '',
cp_1 : '',
cp1_hp : '',
cp1_jpos: '',
cp_2 : '',
cp2_hp : '',
cp2_jpos : '',
quot_num : '',
quot_date : '',
note : ''
}
this.handleProject_type = this.handleProject_type.bind(this);
this.handleStatus = this.handleStatus.bind(this);
this.handleSubject = this.handleSubject.bind(this);
this.handleEnduser_name = this.handleEnduser_name.bind(this);
this.handleLocation = this.handleLocation.bind(this);
this.handleCP1 = this.handleCP1.bind(this);
this.handleCP1_hp = this.handleCP1_hp.bind(this);
this.handleCP1_jpos = this.handleCP1_jpos.bind(this);
this.handleCP2 = this.handleCP2.bind(this);
this.handleCP2 = this.handleCP2.bind(this);
this.handleCP2_hp = this.handleCP2_hp.bind(this);
this.handleCP2_jpos = this.handleCP2_jpos.bind(this);
this.handleQuot_num = this.handleQuot_num.bind(this);
this.handleQuot_date = this.handleQuot_date.bind (this);
this.handleNotes = this.handleNotes.bind(this);
}
handleProject_type(event){
this.setState ({
project_type : event.target.value
})
}
handleStatus(event){
this.setState({
status: event.target.value
})
}
handleSubject(event){
this.setState({
subject: event.target.value
})
}
handleEnduser_name (event) {
this.setState({
enduser_name: event.target.value
})
}
handleLocation(event){
this.setState({
location: event.target.value
})
}
handleCP1(event){
this.setState({
cp_1: event.target.value
})
}
handleCP1_hp(event){
this.setState ({
cp1_hp : event.target.value
})
}
handleCP1_jpos(event){
this.setState ({
cp1_jpos : event.target.value
})
}
handleCP2(event){
this.setState({
cp_2: event.target.value
})
}
handleCP2_hp(event){
this.setState ({
cp2_hp : event.target.value
})
}
handleCP2_jpos(event){
this.setState ({
cp2_jpos : event.target.value
})
}
handleQuot_num(event) {
this.setState ({
quot_num : event.target.value
})
}
handleQuot_date (event) {
this.setState({
quot_date : event.target.value
})
}
handleNotes(event) {
this.setState({
note : event.target.value
})
}
submitEndUser(event){
event.preventDefault();
fetch('http://localhost:4000/enduser', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
})
.then ((response) => {
return response.json;
})
.then ((body) => {
console.log(body);
})
.catch((error) => {
console.log(error)
})
}
和我在 server.js 中的 API:
app.post('/enduser', (req, res) => {
let POST_ENDUSER_QUERY = {
project_id: req.body.project_id,
project_type: req.body.project_type,
status: req.body.status,
subject: req.body.subject,
endUser_name: req.body.endUser_name,
location: req.body.location,
cp_1: req.body.cp_1,
cp1_hp: req.body.cp1_hp,
cp1_jpos: req.body.cp1_jpos,
cp_2: req.body.cp_1,
cp2_hp: req.body.cp1_hp,
cp2_jpos: req.body.cp1_jpos,
quot_num: req.body.quot_num,
quot_date: req.body.quot_date,
note: req.body.note,
image: req.body.image,
estimated_value: req.body.estimated_value
};
if (!POST_ENDUSER_QUERY) {
return res.status(400).send({ err: true, message: 'Please project name' });
}
let query = `INSERT INTO enduser_tbl (project_id, project_type, status, subject, endUser_name, location, cp_1, cp1_hp, cp1_jpos, cp_2, cp2_hp, cp2_jpos, quot_num,quot_date, note, image, estimated_value) VALUES (
'${POST_ENDUSER_QUERY.project_id}',
'${POST_ENDUSER_QUERY.project_type}',
'${POST_ENDUSER_QUERY.status}',
'${POST_ENDUSER_QUERY.subject}',
'${POST_ENDUSER_QUERY.endUser_name}',
'${POST_ENDUSER_QUERY.location}',
'${POST_ENDUSER_QUERY.cp_1}',
'${POST_ENDUSER_QUERY.cp1_hp}',
'${POST_ENDUSER_QUERY.cp1_jpos}',
'${POST_ENDUSER_QUERY.cp_2}',
'${POST_ENDUSER_QUERY.cp2_hp}',
'${POST_ENDUSER_QUERY.cp2_jpos}',
'${POST_ENDUSER_QUERY.quot_num}',
'${POST_ENDUSER_QUERY.quot_date}',
'${POST_ENDUSER_QUERY.note}',
'${POST_ENDUSER_QUERY.image}',
'${POST_ENDUSER_QUERY.estimated_value}'
)`;
dbConn.query(query, ( err, results) => {
if(err) throw err;
return res.status(200).json({"status": 200, "err" : null, "data": results});
});
});
【问题讨论】:
-
请阅读sqlMessage,你已经可以看到根本原因了。
-
表示问题无关。你是对的!
-
@user207421 这个问题让我很困惑,我尝试使用邮递员测试 POST API,sql 和连接都没有错误。