[英]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.