【问题标题】:Error sending data by axios.post () and receiving it within a function in Node.js通过 axios.post() 发送数据并在 Node.js 中的函数中接收数据时出错
【发布时间】:2021-02-04 04:52:40
【问题描述】:

我将 axios.post() 中的数据发送到 Node.js 中的函数。该函数不接受数据的所有参数。 我的错误是什么??

这是 React 中的代码:

            axois.post('http://localhost:9000/question/create', **question**)
            .then((res) => {
                console.log('res.data -> ', res.data);  // The res.data sends all the data (see it in the console)
            }).catch((error) => {
                console.log('error -> ', error);
            });

这是 Node.js 中的代码:

    console.log('req -> ', req.body); // The req.body didn't get all the parameters of the data that came to it.
  dataQuestion.create(**req.body**, (error, data) => {
      if (error) {
          return next(error)
      } else {
          console.log(data)
          res.json(data)
      }
  });
}); ```

Appreciates any answer ...
Thank you!

【问题讨论】:

    标签: node.js reactjs mongodb api axios


    【解决方案1】:

    使用 axios 发帖,需要第二个参数,即帖子请求的正文

    axios.post('/user', {
        firstName: 'Fred',
        lastName: 'Flintstone'
      })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });
    

    【讨论】:

      猜你喜欢
      • 2017-02-23
      • 2012-08-22
      • 1970-01-01
      • 1970-01-01
      • 2015-04-21
      • 2015-07-07
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多