1
votes

I came across an unusual behaviour of the function to upload Firestore documents. In the code below, the values that are updated in document are keyValue : valueValue instead of accessing the constants that store the desired fields to be updated in the Firestore document.

it throws an error: 'keyValue' is declared but its value is never read . That means the actual value "keyValue" is used instead of the constant keyValue.

Any advice?


function createObjectDocument(document: any, caseNumber: String)

{
    document.get().then(function(doc: any) {
        if (doc.exists) {
            console.log("Document data:", doc.data());
            for (let [key, value] of Object.entries(doc.data())) {
                let keyValue = key;
                let valueValue = value;
                console.log(`${key}: ${value}`);
                if (key != "CaseRefArray") {
                    db.collection("casesToExport").doc(caseNumber).update({
                            keyValue: valueValue
                        })
                        .then(() => {
                            console.log("Successfully updated document!");
                        }).catch((error: any) => {
                            console.error("Error updating document: ", error);
                        });
                }
            }
        } else {
            console.log("No such document!");
        }
    }).catch(function(error: any) {
        console.log("Error getting document:", error);
    });

}
1

1 Answers

3
votes

From what I understand, you are trying to send an object { } and setting the object's key using a variable instead creates an object whose key is the name of the variable as opposed to the content of that variable. A quick fix to this would be to encapsulate the keyValue in an array and pass an object to .update() like such: { [keyValue]: valueValue }, a feature that has been added in ES6. I recommend taking a look at the MDN docs as there are plenty of valuable information for any JavaScript developers.