Firebase 更新检索到的数据
Firebase updating the retrieved data
我正在练习 firebase firestore,我正在尝试根据条件更新我检索到的数据,但我收到了这个错误
FirebaseError: Expected type 'rc', but it was: a custom oc object
if (updateMerge === "update") {
const q = query(
collection(db, "contacts"),
where("fname", "==", firstName, "lname", "==", lastName)
);
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
console.log(doc.id, " => ", doc.data());
const payload = { phone: phone };
setDoc(q, payload);
});
}
我正在做一个联系人应用
我可以在控制台上看到检索到的数据,如果用户已经有一个帐户,我想更改 phone 号码,因此有效负载会更改
phone(来自 firestore 文档字段):phone(phone 状态)。
所以我用 setDoc 做了一些练习,但我通常使用“collectionRef”而不是查询,如果我能看到控制台,很可能错误会在这里
setDoc(q, payload);
提前致谢
setDoc()
takes a DocumentReference as first parameter. If you are trying to update the documents in the QuerySnapshot,尝试重构代码如下所示:
querySnapshot.forEach((doc) => {
console.log(doc.id, " => ", doc.data());
const payload = { phone: phone };
// doc.ref is DocumentReference
setDoc(doc.ref, payload);
});
如果您要更新 500 个或更少的文档,那么您可以使用 Batched Writes 来确保所有文档都已更新或更新失败:
import { writeBatch } from "firebase/firestore";
const batch = writeBatch(db);
querySnapshot.forEach((doc) => {
console.log(doc.id, " => ", doc.data());
const payload = { phone: phone };
// doc.ref is DocumentReference
batch.update(doc.ref, payload);
});
batch.commit().then(() => {
console.log("Documents updated")
}).catch((e) => {
console.log("An error occured")
})
我正在练习 firebase firestore,我正在尝试根据条件更新我检索到的数据,但我收到了这个错误
FirebaseError: Expected type 'rc', but it was: a custom oc object
if (updateMerge === "update") {
const q = query(
collection(db, "contacts"),
where("fname", "==", firstName, "lname", "==", lastName)
);
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
console.log(doc.id, " => ", doc.data());
const payload = { phone: phone };
setDoc(q, payload);
});
}
我正在做一个联系人应用 我可以在控制台上看到检索到的数据,如果用户已经有一个帐户,我想更改 phone 号码,因此有效负载会更改 phone(来自 firestore 文档字段):phone(phone 状态)。 所以我用 setDoc 做了一些练习,但我通常使用“collectionRef”而不是查询,如果我能看到控制台,很可能错误会在这里
setDoc(q, payload);
提前致谢
setDoc()
takes a DocumentReference as first parameter. If you are trying to update the documents in the QuerySnapshot,尝试重构代码如下所示:
querySnapshot.forEach((doc) => {
console.log(doc.id, " => ", doc.data());
const payload = { phone: phone };
// doc.ref is DocumentReference
setDoc(doc.ref, payload);
});
如果您要更新 500 个或更少的文档,那么您可以使用 Batched Writes 来确保所有文档都已更新或更新失败:
import { writeBatch } from "firebase/firestore";
const batch = writeBatch(db);
querySnapshot.forEach((doc) => {
console.log(doc.id, " => ", doc.data());
const payload = { phone: phone };
// doc.ref is DocumentReference
batch.update(doc.ref, payload);
});
batch.commit().then(() => {
console.log("Documents updated")
}).catch((e) => {
console.log("An error occured")
})