简体   繁体   English

使用reactjs向节点路径发送axios POST方法的流程是什么?

[英]What is the process for sending a axios POST method using reactjs to a node path?

I am trying to send a POST request using axios to the backend but it is throwing a 404 for the path and i dont know why我正在尝试使用 axios 向后端发送 POST 请求,但它为路径抛出 404,我不知道为什么

Here is the react/redux code calling the axios request这是调用 axios 请求的 react/redux 代码

export const addGoal = (newGoal: Goal) => {
  return (dispatch: any) => {
    authMiddleWare(history)
    const newValues = newGoal
    const authToken = localStorage.getItem('AuthToken')
    axios.defaults.headers.common = { Authorization: `${authToken}` }
    axios
      .post('/goal', newValues)
      .then((response) => {
        console.log('success', response.data)
        dispatch({
          type: ADD_GOAL,
          payload: response.data,
        })
      })
      .catch((err) => {
        console.error('\nCould not submit goal\n', err.response)
      })
  }
}

This is the nodejs path i have in my main backend file for calling the paths这是我在主后端文件中用于调用路径的 nodejs 路径

app.post("/goal", auth, postOneGoal);

This is the backend function for the node path这是节点路径的后端功能

// ADDS A SINGLE WORKOUT

exports.postOneGoal = (request, response) => {
  if (request.body.id.trim() === "" || request.body.text.trim() === "") {
    return response.status(400).json({ body: "Must not be empty" });
  }

  const newGoalItem = {
    username: request.user.username,
    id: request.body.id,
    text: request.body.text
  };

  db.collection("goals")
    .add(newGoalItem)
    .then((doc) => {
      const responseNewGoalItem = newGoalItem;
      responseNewGoalItem.id = doc.id;
      doc.update(responseNewGoalItem);
      return response.json(responseNewGoalItem);
    })
    .catch((err) => {
      response.status(500).json({ error: "Couldn't add the goal" });
      console.error(err);
    });
};

I am using a firebase url proxy in my package.json as well.我也在 package.json 中使用了 firebase url 代理。

Let me know if any more info is needed如果需要更多信息,请告诉我

Posting this as Community Wiki, based in the comments.根据评论将此作为社区 Wiki 发布。

Considering the fact that you are using Cloud Functions, you will need to redeploy the functions everytime you update your code.考虑到您使用的是 Cloud Functions,每次更新代码时都需要重新部署这些函数。 You can check more details on deploying your functions in the official documentation accessible here .您可以在此处访问的官方文档中查看有关部署函数的更多详细信息。 There you will have the options regarding how and where you can deploy your functions for better testing.在那里,您可以选择如何以及在何处部署功能以进行更好的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM