【发布时间】:2020-08-20 06:26:44
【问题描述】:
NodeJS 上的 Firestore 存在问题(函数)。我有一个包含以下内容的 API:
export const getPost = (req: any, res: any): any => {
let postData: any = {};
postData.postId = req.params.postId;
db.collection("posts").doc(`${req.params.postId}`)
.get()
.then((doc) => {
if (!doc.exists) {
return res.status(404).send({ error: "Post not found", success: false });
}
if( doc.data() && postData.postId)
{
postData = doc.data();
postData.postId = req.params.postId;
let commentsRef = db.collection("comments");
return commentsRef.orderBy("createdAt", "desc").where("postId", "==", req.params.postId).get();
}
})
.then((data) => {
if(data){
postData.comments = [];
data.forEach((doc: any): any => {
postData.comments.push({ ...doc.data(), commentId: doc.id });
});
}
return res.status(200).send({ data: postData, success: true });
})
.catch((err) => {
res.status(500).send({ error: err, success: false });
});
};
目前,没有 cmets。有帖子。然而我得到了一个错误。
[Error: Request failed with status code 500]
我错过了什么吗?老实说,我很困惑。
谢谢
【问题讨论】:
-
Firebase 控制台中还有其他错误吗?
-
firebase 控制台中的错误 500。没有理由。 API 中的其他函数运行良好。
-
你能添加日志并检查它在函数中的哪个步骤失败了吗?
-
会的。我感觉它在 cmets 收集步骤中失败了。我会看看我是否可以在没有它的情况下只返回帖子数据以尝试定位更多。
标签: reactjs firebase google-cloud-firestore google-cloud-functions