我无法从 Firestore 中的文档字段获取单个值。检索到后,我需要将其设置为 Firestore 中另一个文档的另一个字段。
客户数量:6
import firebase from "firebase/app";
import "firebase/firestore";
const db = firebaseApp.firestore();
const customerCount = db.collection("customers").doc("--stats--");
const customersCollection = db.collection("customers");
const customerRef = customersCollection.doc(docRef.id);
const batch = db.batch();
batch.update(customerRef, {
customerCount: //<---get the value from firestore (see uploaded image), and set it here
});
batch.set(customerCount, { customerCount: increment }, { merge: true });
batch.commit();
TIA
答案 0 :(得分:0)
您的代码尚未读取 customerCount
文档。现在的 customerCount
只是对文档的引用,但还不包含来自数据库的任何数据。
customerCount.get().then((doc) => {
const batch = db.batch();
batch.update(customerRef, {
customerCount: doc.data().customerCount + 1
});
batch.commit();
});
请注意,这里没有理由使用批处理操作。事实上,如果您想确保 customerRef
和 customerCount
文档始终匹配,在这种情况下,您需要改为 use a transaction。