简体   繁体   English

Firestore addDoc 错误:文档参考无效。 文档引用必须有偶数个段,但

[英]Firestore addDoc error:Invalid document reference. Document references must have an even number of segments, but

I would like to save my data to firestore in React.js app but Firestore returns me Invalid document reference.我想将我的数据保存到 React.js 应用程序中的 firestore,但 Firestore 返回我无效的文档引用。 Document references must have an even number of segments, but文档引用必须有偶数个段,但

Firebase Config file Firebase 配置文件

import { initializeApp,} from "firebase/app";
import {getAuth} from 'firebase/auth'
import {getFirestore} from "firebase/firestore"

const firebaseConfig = {
    apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
    authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
    projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
    storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
    messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
    appId:process.env.REACT_APP_FIREBASE_APP_ID,
    measurementId: process.env.REACT_APP_FIREBASE_MEASUREMENT_ID
  };

  const app =initializeApp(firebaseConfig);
  export const db=getFirestore(app);
  export const auth= getAuth(app)

and it is my code for saving data to firebase with addDoc这是我使用 addDoc 将数据保存到 firebase 的代码

    try{ 
        const user = await createUserWithEmailAndPassword(auth,email,password);
         const data={
            userId:user.user.uid,
            name: "Pls set name",
            about:"Pls set about",
            profileImg:"",
            contacts:[
                "",
            ]
        }
        const docRef =doc(collection(db,`/users/`));
        await addDoc(docRef,data);
        return user
    }catch(err){
        console.log(err.message)
    }

lastly screenshot of error最后是错误截图

在此处输入图像描述

It's customary to use the user's UID as the document ID when creating per-user documents, so that they are easy to find later for that user.在创建每个用户的文档时,习惯上使用用户的 UID 作为文档 ID,以便该用户以后可以轻松找到它们。 Your code is currently generating two random strings for document IDs, which is definitely not what you want.您的代码当前正在为文档 ID 生成两个随机字符串,这绝对不是您想要的。 The first random ID is coming from your call to doc() , then again from your call to addDoc() .第一个随机 ID 来自您对doc()的调用,然后再次来自您对addDoc()的调用。

To use the UID as the document ID, follow along with the documentation :要将 UID 用作文档 ID,请遵循文档

await setDoc(doc(db, "users", user.user.uid), data);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “FIRESTORE 内部断言失败:文档引用无效。文档引用必须有偶数个段,但 NewGame 有 1 个” - "FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but NewGame has 1" 未捕获的 FirebaseError:无效的文档引用。 文档引用必须有偶数个段,但 todos 有 1 - Uncaught FirebaseError: Invalid document reference. Document references must have an even number of segments, but todos has 1 Flutter: PlatformException(error, Invalid document reference. Document references must have an even number of segments, but<x> 有 1,空)</x> - Flutter: PlatformException(error, Invalid document reference. Document references must have an even number of segments, but <x> has 1, null) 文档引用在集合引用上必须有偶数段错误 - Document references must have an even number of segments error on a collection reference 集合引用无效。 集合引用必须有奇数个段 - Invalid collection reference. Collection references must have an odd number of segments Firestore 错误:文档必须具有偶数个路径元素 - Firestore error: A document must have an even number of path elements 集合引用必须有奇数个段 - Collection references must have an odd number of segments Firebase FireStore:如何在使用 addDoc() 添加文档后获取文档快照 - Firebase FireStore : How to get document snapshot after adding document with addDoc() Firebase - 即使没有添加文档,addDoc() 也返回成功 - Firebase - addDoc() returns success even when no document was added 具有集合的 firestore 查询文档包含一个文档 - firestore query document that have a collection contains a document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM