TypeError:使用axios

时间:2019-10-04 12:09:53

标签: javascript node.js axios

我遇到了一个非常奇怪的问题,我没有找到在线答案。

这是我的方法:

export const loginUser = (handleOrEmail, password) => {
  const config = {
    headers: {
        "Content-Type": "application/json"
    }
  };

  const body = JSON.stringify({ handleOrEmail, password });

  try {
    const { data } = await axios.post(
      "http://localhost:5000/api/auth",
      body,
      config
    );

    console.log("data received!", data);
  } catch (err) {
    console.log(
      "Err caught logging in!",
      (err.response && err.response.data) || err
    );
  }
};

我的节点路线:

router.post("/", (req, res) => {
  console.log("entered?");
  const { handleOrEmail, password } = req.body;

  return res.json({ msg: "backend reached!", handleOrEmail, password });
});

它打印entered,但是req.body未定义。为什么会这样呢? GET请求有效(显然,POST请求也有效,但是主体是未定义的。)

0 个答案:

没有答案