将批处理文档写入 Firebase Firestore V9 Angularfire 时出现 "Invalid doc ref. Doc ref must have an even number of segments," 错误

Getting "Invalid doc ref. Doc ref must have an even number of segments," Error when writing batch doc to Firebase Firestore V9 Angularfire

我尝试使用 writeBatch() 在 subcollections 中写入文档。

我跟着 Fireabase Documentation 想出了脚本:

const batch = writeBatch(this.db);

const ref = doc(this.db, 'collection/document/collection1/document/collection2');

batch.set(ref, data);

await batch.commit()

"ERROR 错误:未捕获(承诺):FirebaseError:[code=invalid-argument]:无效文档引用。文档引用必须有偶数个段,但 collection/document/collection1/document/collection2 有 5 个。 “

我在一篇文章中读到 文档的路径有偶数个段。而 collection 的路径有奇数。所以,我决定尝试使用 collection。

const batch = writeBatch(this.db);

const ref = collection(this.db, 'collection/document/collection1/document/collection2');

batch.set(ref, data);

await batch.commit()

但是这次出现类型错误:

“'CollectionReference' 类型的参数不能分配给 'DocumentReference' 类型的参数。 属性 'type' 的类型不兼容。"

经过一番搜索,我在 Firebase documentation 中找到了一个解决方案,您可以使用 auto-generated ID 创建文档引用,然后稍后使用该引用。像这样:

const batch = writeBatch(this.db);

const ref = collection(this.db, 'collection/document/collection1/document/collection2');

batch.set(doc(ref), data);

await batch.commit()