我有自己的API服务器,该服务器已连接到mysql db。当我向服务器发送请求时,一切正常,我在控制台中看到来自浏览器的输入和来自db的输出。但是我在浏览器中看不到任何新内容。我想要更新值,但什么也没有。Image of browser and server console或When I want to print state it won't get any update浏览器
//Exports...
componentDidMount(){
//Fetching number of affected and changed rows
fetch('/api/update')
.then(res => res.json())
.then(rows => this.setState({rows}));
//Fetching output from user query
fetch('/api/query',
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
})
.then(res => res.json())
.then(qo => this.setState({queryOutput: qo}));
}
//Printing 'values'...
服务器
//Some requires and uses...
app.post('/api/query', (req, res) => {
console.log(`\n Query input : ${JSON.stringify(req.body)}`);
let queryInput = (Object.values(req.body).join(' '));
if(queryInput.length>4){//provisional condition
res.json(dbApi.queryFromUser(queryInput));
}
else{
res.json(dbApi.queryOutput);
}
});
MySQL连接
//Connection and others...
const receivingQuery =(queryInput) => {
db.query(queryInput, (err, result) =>{
if(err) throw err+' : '+queryInput;
queryOutput = result;
console.log("\nQuery output "+ JSON.stringify(queryOutput));
});
return queryOutput;
}
module.exports={ queryFromUser: receivingQuery }
检查“ this.setState({rows})”。这是不对的。您指的是什么状态?
用于调试,请在console.log中登录
.then(qo => this.setState({queryOutput: qo}));
即
.then(qo => console.log(qo));