Hey guys. I want to display my query result on my page AFTER I click submit button of a form.
My backend works as I tested with POSTMAN.
//selection
app.post('/select', multerParse.none(),(req,res)=>{
let sql = 'SELECT * FROM users WHERE username = ?'
db.query(sql, [req.body.username], (err,result)=>{
console.log(req.body)
if(err) throw err;
console.log(JSON.stringify(result))
})
})
But something wrong with my frontend.
handleSelection = event =>{
const data = new FormData(event.target)
fetch('http://localhost:5000/select',{
method: 'POST',
body: data
})
.then(response=>{
this.setState({data: response.data})
})
}
I guess it might be that once I click submit, the page refreshes and my state changes back. Is there a way to display my query result after I click?
I used a third-party Component called JsonToTable to quickly render tables.
<form onSubmit={this.handleSelection} >
<h2>Select User</h2>
<br/>
<input name='username' placeholder='Please enter username' type='text'/>
<br/>
<button>Submit</button>
</form>
<JsonToTable json={this.state.data}/>
</div>
Thank you