![](/img/trans.png)
[英]How to replace autogenerated firebase node id to the current user email
[英]how to created autogenerated doc id using batch in firebase 9?
如何使用带有自动生成 ID 的Firebase 9 模块添加多个文档? 我有以前版本的逻辑,但我不能让它与 9 一起使用。请注意,我需要添加一个不存在的文档,此时批量支持、添加还是仅设置?
array.forEach((doc) => { var docRef = db.collection("col").doc(); //automatically generate unique id batch.set(docRef, doc); });
在本指南中针对批量写入说明,如果您不需要读取操作集中的任何文档,则可以将多个写入操作作为包含 set()、update() 或 delete() 的任意组合的单个批处理执行( )操作。
要使用自动生成的 ID 在 Firebase 添加批次,您可以按照以下代码操作:
import { writeBatch, collection, doc } from "firebase/firestore";
// Get a new write batch
const batch = writeBatch(db);
// Add a new document with a generated id
let ref = doc(collection(db, "user"))
batch.set(ref, {name: "Test user"});
// Commit the batch
await batch.commit();
console.log("Document written with generated ID: ", ref.id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.