无法获取使用Admin SDK Firebase的用户列表

问题描述 投票:1回答:1

如果我添加到Firebase云功能,我无法获得用户列表。我已经尝试了很多东西,并遵循firebase文档指南,但它只是继续运行,但从不加载。

exports.listAllUsers = functions.https.onRequest((data, context) => {
  // List all users

  return listAllUsers();
});

function listAllUsers() {
  // List batch of users, 1000 at a time.
  var allUsers = [];

  return admin.auth().listUsers()
    .then(function (listUsersResult) {
      listUsersResult.users.forEach(function (userRecord) {
        // For each user
        var userData = userRecord.toJSON();
        allUsers.push(userData);
      });
        return allUsers
    })
    .catch(function (error) {
      console.log("Error listing users:", error);
    });

}
node.js firebase firebase-authentication google-cloud-functions firebase-admin
1个回答
3
投票

您似乎混淆了两种类型的云功能:

  1. 您可以使用Firebase SDK从应用调用的可调用功能。
  2. 常规HTTP功能,您可以从应用程序调用客户端平台的常规HTTP客户端API。

Cloud Functions that are invoked with regular HTTPS requests

将函数声明为functions.https.onRequest时,需要将响应写入响应对象。基于documentation on calling functions through HTTP requests,你需要做:

exports.listAllUsers = functions.https.onRequest((req, res) => {
  // List batch of users, 1000 at a time.
  var allUsers = [];

  return admin.auth().listUsers()
    .then(function (listUsersResult) {
      listUsersResult.users.forEach(function (userRecord) {
        // For each user
        var userData = userRecord.toJSON();
        allUsers.push(userData);
      });
      res.status(200).send(JSON.stringify(allUsers));
    })
    .catch(function (error) {
      console.log("Error listing users:", error);
      res.status(500).send(error);
    });
});

Calling Cloud Functions that are invoked using the Firebase SDK

如果你想要call your Cloud Function from within your app using the Firebase SDK,你需要将你的函数声明为:

exports.listAllUsers = functions.https.onCall((data, context) => {
  // List all users

  return listAllUsers();
});
© www.soinside.com 2019 - 2024. All rights reserved.