是否可以通过firestore将要更新的字段作为变量传递?
我想创建一个函数来更新文档,比如...
updateFirebaseDocument('enquiries', 'asdaasdasds, 'status', '1'))
使用以下函数
export async function updateFirebaseDocument(collectionName, documentId, field, updateValue) {
var doc = db.collection(collectionName).doc(documentId)
return doc.update({
field: updateValue
})
.then(function() {
console.log("Document successfully updated!");
})
.catch(function(error) {
// The document probably doesn't exist.
console.error("Error updating document: ", error);
});
}这是可行的,但问题是,它创建了一个名为field的字段,而不是更新状态字段。有没有一种方法可以做到这一点而不是硬编码更新字段?
发布于 2020-07-12 21:39:39
您可以不使用ES6
export async function updateFirebaseDocument(collectionName, documentId, field, updateValue) {
var doc = db.collection(collectionName).doc(documentId)
var obj = {}
obj[field] = updateValue;
return doc.update(obj)
.then(function() {
console.log("Document successfully updated!");
})
.catch(function(error) {
// The document probably doesn't exist.
console.error("Error updating document: ", error);
});
}发布于 2020-07-12 21:18:33
使用方括号为我解决了这个问题。
[field]: updateValuehttps://stackoverflow.com/questions/62861572
复制相似问题