2017-06-14 89 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); 
    } 
    }) 
}) 
相關問題