Can't get list of users using Admin SDK Firebase

女生的网名这么多〃 提交于 2019-12-02 04:18:00

You seem to be confusing two types of Cloud functions:

  1. Callable functions, that you call from your app using the Firebase SDK.
  2. Regular HTTP functions, that you call from your app your client platform's regular HTTP client API.

Cloud Functions that are invoked with regular HTTPS requests

When you declare your function as functions.https.onRequest, you need to write your response to the response object. Based on the documentation on calling functions through HTTP requests, you'll need to do:

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

If you want to call your Cloud Function from within your app using the Firebase SDK, you need to declare your function as:

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

  return listAllUsers();
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!