【问题标题】:Why is there no data in my POST request response?为什么我的 POST 请求响应中没有数据?
【发布时间】:2019-08-30 21:09:12
【问题描述】:

我正在向我使用 express 设置的路线发出发布请求:

todolists.post('/getTodoLists', (req, res) => {
  TodoList.findOne({
    userEmail: req.body.userEmail
  })
    .then(todolist => {
      if (!todolist) {
        res.status(404).send()
      } else {
        res.status(200).json(todolist)
      }
    })
    .catch(err => {
      console.log(err)
      res.status(500).send(err)
    })
})

当我使用以下代码从数据库中检索数据时,我在控制台中得到的只是'object Object'

axios
  .post('todo-lists/getTodoLists', {
      userEmail: this.userEmail
   })
     .then(res => {
        console.log('response from gettodoLists' + res)
        this.todolist = res
        console.log(this.todolist)
      })
      .catch(err => {
        console.log(err)
      })

当我使用 Postman 向具有相同请求数据的同一路由发出请求时,我得到了我希望收到的所有数据。

提前致谢!

编辑:

这是我从邮递员那里得到的

{
    "_id": "5caca1498accb128c8974d56",
    "title": "todolist1",
    "userEmail": "test@gmail.com",
    "todos": [
        {
            "_id": "5caca1498accb128c8974d57",
            "description": "Get this done",
            "completed": true
        }
    ],
    "dateDue": "2019-04-07T18:24:31.207Z",
    "__v": 0
}

这是我在控制台记录响应时在控制台中得到的内容

response from gettodoLists[object Object]

【问题讨论】:

标签: express post axios


【解决方案1】:

您必须从响应对象中获取数据才能在 Postman 中看到正文。

axios.post('todo-lists/getTodoLists', {
      userEmail: this.userEmail
   })
     .then(res => {
        console.log(res.data)
      })
      .catch(err => {
        console.log(err)
      })

响应是包含标头等的完整响应

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多