Firestore v9 子集合路径修正:创建正确的评论子集合结构
修正Firestore子集合路径问题
你的问题出在collection()方法的参数上——你重复传入了两次id.toString(),导致路径多了一层重复的帖子ID层级。
要得到预期的/comments/380498/0PDacNW1K70gAPBDWaXk路径,只需要删除重复的id.toString()参数即可:
修正后的代码:
const details = { name, comment, mail, id }; const dbRef = collection(db, "comments", id.toString()); addDoc(dbRef, details) .then(() => { res.end("comment done"); }) .catch((error) => { res.end("Internal server error try again later"); });
修改后,addDoc()会在comments/帖子ID这个子集合下自动生成唯一的文档ID,正好匹配你期望的路径结构。
内容的提问来源于stack exchange,提问作者gaito Uzumaki
相关产品推荐
相关产品推荐

