【发布时间】:2019-12-30 12:09:38
【问题描述】:
取自 Google Firebase 网站的官方指南文档,假设我要更新一个文档。
var washingtonRef = db.collection("cities").doc("DC");
// Set the "capital" field of the city 'DC'
return washingtonRef.update({
capital: true
})
.then(function() {
console.log("Document successfully updated!");
})
.catch(function(error) {
// The document probably doesn't exist.
console.error("Error updating document: ", error);
});
如您所见,我可以在集合括号、文档括号和值中放置一个变量,我想分配给我的字段。但是,我不能使用变量作为字段名称。如果我写这样的东西
var collection = "people";
var document = "john";
var value = 5;
var field = "result";
var docRef= db.collection(collection).doc(document);
return docRef.update({
field: value
})
.then(function() {
console.log("Document successfully updated!");
})
.catch(function(error) {
// The document probably doesn't exist.
console.error("Error updating document: ", error);
});
,除了字段变量之外,它都可以工作。在firestore数据库中,它将更新“people”集合中“john”文档中名为“field”的字段(或者更像是创建一个新字段),编号为5。我在尝试什么实现的方法是在“people”集合中有一个“john”文档,其中包含一个名为“result”的字段,其值为 5。
我想要一个函数来获取所有 4 个变量并更新某个集合中某个文档中的某个字段。这可能吗?有人知道解决这个问题的方法吗?
谢谢大家的回答。
【问题讨论】:
标签: javascript firebase google-cloud-firestore