2

I'm trying to check if a Map contains a value. I tested this code in typescript playground.

let userMap = new Map();

userMap.set("user1", 'displayName');
userMap.set("user2", 'displayName2');

        console.log(friendsMapNew.has("user2")); //true

But when I use this code in a google cloud function I get this error:

error TypeError: userMap.has is not a function

This is the code I use in the cloud function.

let userMap = new Map();
userMap = getUserDocument.data()['userMap']; //if i console.log this, it shows a map like this:
{ K79DFS: 'Bob' }
if (userMap.has("Bob") === true) {console.log("user exists")}

What am I doing wrong?

Thanks!

1 Answer 1

3
getUserDocument.data()['userMap'];

This statement returns an object and not a map.

And object doesn't have .has() function. Hence the error.

You should use this instead if "Bob is a value" in your object

Object.values(userMap).includes("Bob") === true

To check for the property name you can use:

Object.keys(userMap).includes("K79DFS") === true
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Is there also a possibility to 'query' for the other value: here: K79DFS?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.