无法到达POST响应json-ReactJS,NodeJS

问题描述 投票:0回答:2

我有自己的API服务器,该服务器已连接到mysql db。当我向服务器发送请求时,一切正常,我在控制台中看到来自浏览器的输入和来自db的输出。但是我在浏览器中看不到任何新内容。我想要更新值,但什么也没有。Image of browser and server consoleWhen 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 }
javascript node.js reactjs express post
2个回答
0
投票

检查“ this.setState({rows})”。这是不对的。您指的是什么状态?


0
投票

用于调试,请在console.log中登录

.then(qo => this.setState({queryOutput: qo}));

.then(qo => console.log(qo));
© www.soinside.com 2019 - 2024. All rights reserved.