簡體   English   中英

我正在嘗試從 firestore 獲取用戶並將它們存儲在 javascript 的 Map 但由於某種原因 map 是空的

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

所以用戶 Object 有 5 個屬性 firstName、lastName、email、password 和 city。

我正在使用 class 用戶中的方法來獲取用戶,該方法是:

  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;
  }

在 chrome 控制台中返回:

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

我正在嘗試在 Map 中使用憑據,因為每個用戶都將擁有一個唯一的 email,這將是 map 的關鍵。

如果this.users.get()是異步的,則必須在返回usersMap之前await結果。

 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; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM