将响应数据(res.json)从快速路由传递到react axios post方法

时间:2018-07-25 06:45:25

标签: reactjs express axios

 ID  CITY   DATA
 202 Dublin numbertwo
 303 Dublin numberthree
 404 Dublin numberfour

这是我的axios呼叫,它将用户输入传递到我的快速路线。

 An error was encountered performing the requested operation:
 Internal demo could not be converted
 Vendor code 17059

这是使用用户输入查询数据库的快速路由。在这里,我想将数据库查询的结果设置为一个变量,并将其作为响应发送回axios调用。我想做的事甚至有可能吗?

2 个答案:

答案 0 :(得分:0)

你很亲密

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})
})

答案 1 :(得分:0)

await语句应位于async function内。

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})
  }
})