Home > Blockchain >  Using string key to access Firestore map value gives IDE warning that string was never read
Using string key to access Firestore map value gives IDE warning that string was never read

Time:01-25

I have a Firebase Cloud Function that reads a document from Firestore, gets a map field, and gets a boolean value from one of that map's keys. However, my IDE says that the string key I use to get the value from the map (someKey in the code below) is "declared but never read", which puzzles me. Aren't I reading it when I use it to access the map? What am I doing wrong?

exports.someFunction = functions.https.onCall(async (data, _context) => {
    const uid = data.uid;
    const someKey = data.someKey; // function argument; guaranteed to be a string

    const settingsDoc = await admin.firestore().doc("settings/"   uid).get();
    const theMapInQuestion = settingsDoc.get("theMapInQuestion"); // guaranteed to be a Firestore map of type [string: boolean]
    const theBooleanInQuestion = theMapInQuestion.someKey;

    if (theBooleanInQuestion === true) {
        // proceed
    } else {
        ...
    }
});

CodePudding user response:

The message is telling you that you never used the const someKey after it was declared. Indeed, I do not see where that variable is ever used in the code you show here. You could certainly delete the line of code that declares someKey and it would work exactly the same.

The only thing you're doing wrong that I can see is assuming that the const is actually being used when the compiler says it's not. (It's rarely a good idea to doubt the compiler.)

If you intended to access the property with the value of the someKey variable, then you meant to write theMapInQuestion[someKey].

  • Related