如何更新一个消防基地火灾恢复文件

验证后,我试图在/users/上查找一个用户文档,然后我想用 auth 对象的数据以及一些自定义用户属性来更新文档。但是我得到一个错误,更新方法不存在。是否有更新单个文档的方法?所有的 firestordoc 示例都假设您有实际的 doc id,并且它们没有任何使用 where 子句进行查询的示例。

firebase.firestore().collection("users").where("uid", "==", payload.uid)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
doc.update({foo: "bar"})
});
})
172389 次浏览

You can precisely do as follows (https://firebase.google.com/docs/reference/js/v8/firebase.firestore.DocumentReference):

var db = firebase.firestore();


db.collection("users").doc(doc.id).update({foo: "bar"});

Check if the user is already there then simply .update, or .set if not:

    var docRef = firebase.firestore().collection("users").doc(firebase.auth().currentUser.uid);
var o = {};
docRef.get().then(function(thisDoc) {
if (thisDoc.exists) {
//user is already there, write only last login
o.lastLoginDate = Date.now();
docRef.update(o);
}
else {
//new user
o.displayName = firebase.auth().currentUser.displayName;
o.accountCreatedDate = Date.now();
o.lastLoginDate = Date.now();
// Send it
docRef.set(o);
}
toast("Welcome " + firebase.auth().currentUser.displayName);
});
}).catch(function(error) {
toast(error.message);
});

in your original code changing this line

doc.update({foo: "bar"})

to this

doc.ref.update({foo: "bar"})

should work

but a better way is to use batch write: https://firebase.google.com/docs/firestore/manage-data/transactions#batched-writes

You only need to found official ID of document, code here!

    enter code here
//Get user mail (logined)
val db = FirebaseFirestore.getInstance()
val user = Firebase.auth.currentUser
val mail = user?.email.toString()


//do update
val update = db.collection("spending").addSnapshotListener { snapshot, e ->
val doc = snapshot?.documents
doc?.forEach {
//Assign data that I got from document (I neet to declare dataclass)
val spendData= it.toObject(SpendDt::class.java)
if (spendData?.mail == mail) {
//Get document ID
val userId = it.id
//Select collection
val sfDocRef = db.collection("spendDocument").document(userId)
//Do transaction
db.runTransaction { transaction ->
val despesaConsum = hashMapOf(
"medalHalfYear" to true,
)
//SetOption.merege() is for an existing document
transaction.set(sfDocRef, despesaConsum, SetOptions.merge())
}
}


}
}
}
data class SpendDt(
var oilMoney: Map<String, Double> = mapOf(),
var mail: String = "",
var medalHalfYear: Boolean = false
)

correct way to do this is as follows; to do any data manipulation in snapshot object we have to reference the .ref attribute

 firebase.firestore().collection("users").where("uid", "==", payload.uid)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
doc.ref.update({foo: "bar"})//not doc.update({foo: "bar"})
});
})

-- UPDATE FOR FIREBASE V9 --

In the newer version of Firebase this is done like this:

import { doc, updateDoc } from "firebase/firestore";


const washingtonRef = doc(db, "cities", "DC");


// Set the "capital" field of the city 'DC'
await updateDoc(washingtonRef, {
capital: true
});