【问题标题】:Internal server error 500 react post to firebase内部服务器错误 500 对 firebase 做出反应
【发布时间】:2021-06-03 16:04:10
【问题描述】:

我在发布到我的 firebase 数据库时收到 500 错误。但是,当我通过邮递员发帖时,它工作正常,因此我在调试时遇到了很多麻烦。目前,我已经对categoryIdnewRow 进行了硬编码,以确保我的状态没有任何问题。

我认为handleSubmit是唯一相关的函数

  handleSubmit = (event) => {
    event.preventDefault();
    const categoryId = "1RegisterInfo";
    const newRow = {
      index: "3",
      body: "this.state.body",
      dataType: "this.state.dataType",
      visit: "test",
    };
    this.props.postRow(categoryId, { newRow });
  };

动作

export const postRow = (categoryId, rowData) => (dispatch) => {
  dispatch({ type: "LOADING_UI" });
  axios
    .post(`/category/${categoryId}`, rowData)
    .then((res) => {
      dispatch({
        type: "POST_ROW",
        payload: res.data,
      });
      dispatch(clearErrors());
    })
    .catch((err) => {
      dispatch({
        type: "SET_ERRORS",
        payload: err.response.data,
      });
    });
};

云功能

exports.postRow = (req, res) => {
  if (req.body.body.trim() === "") {
    return res.status(400).json({ comment: "Must not be empty" });
  }
  if (req.body.index.trim() === "") {
    return res.status(400).json({ comment: "Must not be empty" });
  }
  if (req.body.dataType.trim() === "") {
    return res.status(400).json({ comment: "Must not be empty" });
  }
  if (req.body.visit.trim() === "") {
    return res.status(400).json({ comment: "Must not be empty" });
  }

  const newRow = {
    index: req.body.index,
    dataType: req.body.dataType,
    visit: req.body.visit,
    body: req.body.body,
    createdAt: new Date().toISOString(),
    categoryId: req.params.categoryId,
    disapproveCount: 0,
    approveCount: 0,
  };

  db.doc(`/categories/${req.params.categoryId}`)
    .get()
    .then((doc) => {
      if (!doc.exists) {
        return res.status(404).json({ error: "Category not found" });
      }
    })
    .then(() => {
      return db.collection("rows").add(newRow);
    })
    .then(() => {
      res.json(newRow);
    })
    .catch((err) => {
      console.log(err);
      res.status(500).json({ error: "Something went wrong" });
    });
};

任何帮助表示赞赏!

【问题讨论】:

    标签: reactjs react-redux google-cloud-functions


    【解决方案1】:

    您没有发送正确的有效负载。

    { newRow }
    

    相同
    {
      newRow: {
        index: '3',
        body: this.state.body,
        dataType: this.state.dataType,
        visit: 'test',
      },
    }
    

    您在请求正文中传递上述数据,因此 req.body.bodyundefined 导致 req.body.body.trim() 失败。

    this.props.postRow(categoryId, { newRow })
    

    应该是

    this.props.postRow(categoryId, newRow)
    

    我建议在尝试执行任何其他操作之前使用Joi 或类似的东西来验证请求负载。

    【讨论】:

    • 合二为一。我现在可以把我的笔记本电脑扔出窗外,我注意到了这个错误,但出于某种原因,我想“不,一个对象应该放在花括号中”,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 2011-10-17
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多