[英]How to update a document on Firestore based on query
I'm creating a web form page which updates the input fields into the corresponding firestore document based on a condition.我正在创建一个 web 表单页面,它根据条件将输入字段更新到相应的 Firestore 文档中。 How can this be done?如何才能做到这一点?
The first part where i get the results of the query is okay, as the console log shows the correct query result.我得到查询结果的第一部分是好的,因为控制台日志显示了正确的查询结果。 However, the second part for updating that document isnt working但是,更新该文档的第二部分不起作用
const reportForm = document.querySelector('#daily-reports');
reportForm.addEventListener('submit', (e) => {
e.preventDefault();
const forward = reportForm['forward'].value;
const reflected = reportForm['reflected'].value;
const upserror = reportForm['ups-switch'].value;
const transerror = reportForm['trans-switch'].value;
const transDesc = reportForm['transDesc'].value;
const upsDesc = reportForm['upsDesc'].value;
console.log(forward, reflected, upserror, transerror, transDesc, upsDesc);
firebase.initializeApp(config);
const db = firebase.firestore();
db.settings({timestampsInSnapshots: true});
firebase.auth().onAuthStateChanged(user => {
if(user) {
console.log(user.uid);
console.log(user.email);
//Query begins here
db.collection('Stations').where('userId', "==", user.uid).get().then(function(querySnapshot) {
querySnapshot.forEach(function (doc) {
doc.data().update(
{
"ForwardPower": forward,
"ReflectedPower": reflected,
"TransDesc": transDesc,
"TransmitterError": transerror,
"UPSError" : upserror,
"UPSDesc": upsDesc}
);
console.log(doc.id, " => ", doc.data());
});
}).catch(function(error) {
console.log("Error getting documents: ", error);
});
}
});
});
After running the code, this is the error I get:运行代码后,这是我得到的错误:
Error getting documents: TypeError: "doc.data(...).update is not a function"
<anonymous> func.js:42
forEach database.ts:2131
forEach document_set.ts:97
inorderTraversal sorted_map.ts:324
inorderTraversal sorted_map.ts:136
forEach document_set.ts:96
forEach database.ts:2130
<anonymous> func.js:41
data() is used on a DocumentSnapshot only to get a hold of its raw contents. data()仅用于 DocumentSnapshot 以获取其原始内容。 The returned object is just a plain JavaScript object and has no methods.返回的 object 只是一个普通的 JavaScript object 并且没有任何方法。
If you want to update the document represented by a DocumentSnapshot, use the ref property of that object to get a DocumentReference , then call the update() method on that object.如果要更新由 DocumentSnapshot 表示的文档,请使用该 object 的ref属性来获取DocumentReference ,然后对该 object 调用update()方法。
doc.ref.update({...})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.