首页 > 解决方案 > 发布请求在节点应用程序上返回 500 错误

问题描述

在我的社交媒体应用程序中,当用户对帖子发表评论时,它会抛出一个500 Internal Server Error.

控制台状态POST https://shielded-journey-88539.herokuapp.com/https://us-central1-myapp-1d191.cloudfunctions.net/api/post/3Y7OcHJXXXa0ilBeq35u/comment 500 (Internal Server Error)

当我检查commentOnPost路线时Postman,响应返回 a Status 200,但正文返回Invalid Host Header

邮递员:对帖子发表评论 邮递员:身体

// Comment on a Post API
exports.commentOnPost = (req, res) => {
  if (req.body.body.trim() === '') {
    return res.status(400).json({ comment: 'Cannot be empty' });
    }

  const newComment = {
    body: req.body.body,
    createdAt: new Date().toISOString(),
    postId: req.params.postId,
    userHandle: req.user.handle,
    profileImage: req.user.profileImage
  };

  db.doc(`/posts/${req.params.postId}`)
    .get()
    .then(doc => {
      if (!doc.exists) {
        return res.status(404).json({ error: 'Post does not exist.' });
            }
            // after gaining access to document, use prefix reference to update comment count
            return doc.ref.update({ commentCount: doc.data().commentCount + 1 })
        })
        .then(() => { // add newComment to comments collection
            return db.collection('comments').add(newComment);
        })
    .then(() => {
      res.json(newComment);
    })
    .catch(err => {
      console.log(err);
      res.status(500).json({ error: 'Something went wrong' });
    });
};

当 Iconsole.log(commentData)在 中时dataSlice/submitComment,它只从路由返回对象req.body.body的其余部分,而不是对象的其余部分。newCommentcommentOnPost

评论数据

// submitComment of dataSlice
export const submitComment = (postId, commentData) => dispatch => {
      console.log(commentData)
        return axios
        .post(`/post/${postId}/comment`, commentData)
        .then(res => {
                dispatch(submitTheComment(res.data))
          dispatch(clearErrors());
        })
        .catch(err => dispatch(setErrors(err.response)))
    };

我正在使用我自己的 Heroku 代理服务器。

// App.jsx
axios.defaults.baseURL =
  'https://shielded-journey-88539.herokuapp.com/https://us-central1-myapp-1d191.cloudfunctions.net/api';

// package.json
"proxy": "https://shielded-journey-88539.herokuapp.com/https://us-central1-myapp-1d191.cloudfunctions.net/api"

我究竟做错了什么?

标签: node.jsherokugoogle-cloud-functionsredux-toolkitproxy-server

解决方案


你可以试试这个代码,和console.log(commentData),和在哪里commentData

exports.commentOnPost = (req, res) => {
  if (req.body.body.trim() === '') {
    return res.status(400).json({ comment: 'Cannot be empty' });
    }
  const newComment = {
    body: req.body.body,
    createdAt: new Date().toISOString(),
    postId: req.params.postId,
    userHandle: req.user.handle,
    profileImage: req.user.profileImage
  };
  
  console.log("newComment: ", newComment)

  db.doc(`/posts/${req.params.postId}`).get()
    .then(doc => {
      if (!doc.exists) {
        return res.status(404).json({ error: 'Post does not exist.' });
      }
        // after gaining access to document, use prefix reference to update comment count
        return doc.ref.update({ commentCount: doc.data().commentCount + 1 });
    }).then(() => { 
        // add newComment to comments collection
            db.collection('comments').add(newComment);
            res.status(200).json(newComment);
    }).catch(err => {
      console.log("Error in Catch commentOnPost: ", err);
      res.status(500).json({ error: 'Something went wrong' });
    });
};

推荐阅读