如何使用新的模块化SDK V9在防火墙中创建具有自定义id的文档?
const register = async function (adminEmail){
try{
const docSnap = await getDoc(availableIdRef);
const collegeId = docSnap.data().id; //This should be the document id
const adminRef = collection(firestore, 'admin');
await addDoc(adminRef, {adminEmail, collegeId});
}catch(e){
alert("Error!");
}
}我想在collegeId集合中创建一个id为admin的新文档。
我知道如何在web 8(命名空间)中这样做,但对web 9(模块化)一无所知。
发布于 2021-10-08 16:55:24
您可以使用setDoc()方法来指定ID,而不是addDoc,这将生成文档中提到的随机ID。
const adminRef = doc(firestore, 'admin', collegeId);
// doc --->^
await setDoc(adminRef, {email: adminEmail, id: collegeId}) // overwrites the doc if it already exists
// ^^^还请注意,要创建DocumentReference,您需要使用doc()而不是用于创建CollectionReference的collection()。我在这里解释了这2种不同之处:修复:在Webv9中添加新数据的模式是什么?
https://stackoverflow.com/questions/69498975
复制相似问题