Reactjs 将响应数据(res.json)从express路由传递到react axios post方法
这是我的axios调用,它将用户输入传递到我的express routeReactjs 将响应数据(res.json)从express路由传递到react axios post方法,reactjs,express,axios,Reactjs,Express,Axios,这是我的axios调用,它将用户输入传递到我的express route axios({ url: 'myurl.com', method: 'post', data:data,}) .then(function(response) { this.setState({auth:response}); //this is where I want to send express response to }).catch(function(err) {
axios({
url: 'myurl.com',
method: 'post',
data:data,})
.then(function(response) {
this.setState({auth:response}); //this is where I want to send express response to
}).catch(function(err) {
console.log(err);
})
下面是使用用户输入查询数据库的快速路由。在这里,我想将db查询的结果设置为一个变量,并将其作为响应发送回axios调用。我想做的是可能的吗?你很接近
app.post('myurl.com', function(req,res) {
const user = req.body.data
const pass = req.body.otherData
const token = await db.call(req,res,user,pass)
\\ res.json = token ?
})
语句应该位于的内部
app.post('myurl.com', function(req,res) {
const user = req.body.data
const pass = req.body.otherData
const token = await db.call(req,res,user,pass)
res.status(200).send({token})
})
app.post('myurl.com', async function(req,res) { //add async to function
//put await statement inside try/catch
try{
const token = await db.call(req,res,user,pass)
res.json({token}) //use json function from resp to send json response
}catch(error){
res.json({error})
}
})