Skip to content
Advertisement

I am trying to get a users from firestore and store them in Map in javascript but for some reasone the map is empty

So the user Object has 5 attributes firstName , lastName,email,password and city.

I am getting the users with a method in class Users which is :

  getUsersData () {
    const usersMap = new Map()
    this.users.get().then((data) => {
      data.docs.forEach(user=> {
        const {email , password} = user.data();
        usersMap.set(email,password);
      });
    });
    return usersMap;
  }

which returns this in chrome console:

Map(0) {}
[[Entries]]
0: {"mehdiboujid@gmail.com" => "dqdqwdq"}
size: (...)
__proto__: Map

I am trying to use credentials in a Map because every user will have a unique email which will be the key to the map.

Advertisement

Answer

If this.users.get() is async you will have to await the result before returning the usersMap.

async getUsersData() {
  const usersMap = new Map()
  await this.users.get()
    .then((data) => {
      data.docs.forEach(user => {
        const { email, password } = user.data();
        usersMap.set(email, password);
      });
    });
  return usersMap;
}
Advertisement