Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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 {
        ...
    }
});

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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].

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading