2017-06-14 85 views
0

我试图做一个GET请求,它将从mySQL数据库触发SELECT查询。但是,我需要这个请求是动态的,因为要查询的数据取决于用户的输入。以下是我想出基于我如何执行POST请求:mySql/Express GET请求到React Native应用程序中的动态SELECT查询

*的handlePress功能获取在其各自的组分选择

handlePress = (inputId) => { 
    fetch('http://127.0.0.1:3000/testData', { 
    "method": "GET", 
    body: JSON.stringify({ 
     id: inputId 
    }) 
    }) 
    .then((response) => response.json()) 
    .then((responseData) => { 
     this.setState({newData: responseData}) 
    }) 
} 


app.get('/testData', function (req, res) { 
    connection.query('select * from ticket_data where id = ' + req.body.id, function(error, results, fields) { 
    if(error) { 
     console.log('Error in GET/query') 
    } else { 
     res.send(results); 
    } 
    }) 
}) 

回答

0

好吧触发,所以我想它了。你必须使用req.query;我这样做的方式只对POST请求有效。如果其他人遇到同样的问题,这里是我的解决方案:

handlePress = (inputId) => { 
    fetch('http://127.0.0.1:3000/testData?id=' + inputID, { 
    "method": "GET" 
    }) 
    .then((response) => response.json()) 
    .then((responseData) => { 
     this.setState({newData: responseData}) 
    }) 
} 


app.get('/testData', function (req, res) { 
    connection.query('select * from ticket_data where id= ' + req.query.id, function(error, results, fields) { 
    if(error) { 
     console.log('Error in GET/query') 
    } else { 
     res.send(results); 
    } 
    }) 
}) 
相关问题