如何从Firebase获取用户邮箱列表?
提取用户邮箱地址的解决方案
你的原代码会输出整个用户对象集合,要只获取邮箱地址,只需遍历用户数据,提取每个对象里的email字段即可。
修改后的代码如下:
function getUsersEmail() { const dbRef = ref(getDatabase()); get(child(dbRef, `users/`)).then((snapshot) => { if (snapshot.exists()) { const users = snapshot.val(); // 提取所有用户的邮箱地址 const emails = Object.values(users).map(user => user.email); // 以表格形式输出邮箱列表 console.table(emails); } else { console.log("No data available"); } }).catch((error) => { console.error(error); }); }
关键逻辑说明
Object.values(users):将以用户ID为键的用户对象,转换为包含所有用户信息的数组.map(user => user.email):遍历数组,从每个用户信息中提取email字段,得到纯邮箱列表- 如果需要同时保留用户ID和邮箱,可以改用以下逻辑:
const userEmailList = Object.entries(users).map(([userId, user]) => ({ 用户ID: userId, 邮箱: user.email })); console.table(userEmailList);
内容的提问来源于stack exchange,提问作者Wyse
相关产品推荐
相关产品推荐

