You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 02:50:57