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

注册后在Firestore存储用户凭证时遇集合引用错误求助

问题解决:Firestore 集合引用错误

错误原因

你用collection(db, 'userDatabase', uid)创建的引用不符合Firestore路径规则:

  • Firestore的路径遵循「集合-文档-集合」交替结构,集合引用的路径段数必须是奇数(比如userDatabase是1段,属于合法集合),而userDatabase/uid是2段,属于文档路径,不能用collection()创建。

修正方案

把创建引用的方法从collection()换成doc()——你需要在userDatabase集合下创建以用户UID为ID的文档,doc()才是用来指向具体文档的方法:

const db = getFirestore(app)
//Sign up User Method
createUserWithEmailAndPassword(auth, email, password)
  .then((res) => {
    // Signed in 
    const uid = res.user.uid;
    const data = {
      id: uid,
      email,
      name,
    };
    // 修正:用doc()替代collection()
    const ref = doc(db, 'userDatabase', uid)
    console.log(ref);
    setDoc(ref, data)
      .then(() => console.log("Created New User Document Successfully"))
      .catch((e) => console.log("Error", e));
  }).catch((e) => console.log(e))

补充说明

  • collection()用于获取集合的引用(比如collection(db, 'userDatabase')指向整个用户集合)
  • doc()用于获取文档的引用,传入三个参数时格式为doc(数据库实例, 集合名, 文档ID),完全匹配你要创建的用户文档路径。

内容的提问来源于stack exchange,提问作者Sir-George S Mulbah Jr.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 13:55:18