使用uid数组从Auth获取Firebase用户

时间:2020-06-05 03:40:45

标签: node.js firebase firebase-authentication

我正在尝试获取用户列表,但是我需要传递UID列表,并且我一直在检查firebase admin.auth文档,发现以下方法:

getUsers(identifiers: UserIdentifier[]): Promise<GetUsersResult>

https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth#getusers

但是当我尝试调用该方法并传递一个用户UID数组时,它显示为admin.auth(...).getUsers is not a function

这是我正在尝试的:

在客户端:

const query = firebase.firestore()
  .collection('users')
  .where('userType', '==', '1');
  .limit(5);

const results = []
query.get().then((collection) => {
  collection.forEach((snapshot) => {
    if (!snapshot.empty) {
      results.push(snapshot);
      }
    });

const userIdList = results.map(doc => doc.id);

axios.post('myAdmin/getUsers', { userIdList })
  .then(result => console.log(result))
  .catch(err => console.log(err));

在服务器端:

const serviceAccount = require('./myServiceAccount');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: 'https://mydatabase-url.firebaseio.com',
});


m.getUsers = async (req, res, next) => {
    const { userIdList } = req.body
    const users = await admin.auth().getUsers(userIdList);
    res.json({
      users,
    });

  };

您知道该方法是否仍然有效?

1 个答案:

答案 0 :(得分:0)

firebase-admin更新到最新版本后,我能够解决它。

我还做了一个更改,将用户ID作为对象数组发送:

const userIdList = results.map(doc => {return { uid: doc.id } });

axios.post('myAdmin/getUsers', { userIdList })
  .then(result => console.log(result))
  .catch(err => console.log(err));