Javascript 更新Firestore字段,但字段是变量

Javascript 更新Firestore字段,但字段是变量,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,是否可以将要更新的字段作为变量传递给firestore 我想创建一个函数来更新文档,例如 updatefirebadedocument('inquiries','asdaasdasds','status','1')) 具有以下功能 export async function updateFirebaseDocument(collectionName, documentId, field, updateValue) { var doc = db.collection(collectionN

是否可以将要更新的字段作为变量传递给firestore

我想创建一个函数来更新文档,例如

updatefirebadedocument('inquiries','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的字段,而不是更新status字段。有没有办法做到这一点而不是硬编码更新字段?

使用方括号为我解决了这个问题

      [field]: updateValue

你可以不用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);
    });
}

术语是
对象的属性或键
,因此我们不会混淆:)。是的,您可以使用
ComputedPropertyName
作为对象文本语法的一部分,如您所演示的。但这只适用于ES6。