node.js - 发布请求在节点应用程序上返回 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
的其余部分,而不是对象的其余部分。newComment
commentOnPost
// 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"
我究竟做错了什么?
解决方案
你可以试试这个代码,和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' });
});
};
推荐阅读
- python - 在python中将csv文件作为字符串读取
- reactjs - 使用来自四个不同选择标签的值更新状态中的对象时出现问题
- bash - 我正在尝试找到一种方法将我当前的 SSID 记录到文档中
- ios - Swift 数组过滤器属性
- amazon-web-services - 如何通过在云端调整图像 lambda 边缘函数来修复 503 错误?
- r - 无法索引使用 seq() 和 rep() 创建的向量的某些值
- c# - 运行多个 Async/Await 调用时的 DBContext 和 AsNoTracking
- angular - Angular cli 7.1.4 提取的 styles.css 文件意外字符
- php - 如何修复 'preg_match(): Compilation failed: invalid range in character class at offset 1828' 错误在 laravel 'url' 验证中?
- svg - 如何在 Easel.js 中缩放 SVG 而不会丢失轮廓?