无法使用Admin SDK Firebase获取用户列表 [英] Can't get list of users using Admin SDK Firebase

查看:69
本文介绍了无法使用Admin SDK Firebase获取用户列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我添加到Firebase云功能,则无法获取用户列表.我尝试了很多事情,并遵循了Firebase文档上的指南,但是它一直运行,但从未加载.

If I add to the Firebase cloud functions, I cannot get a list of users. I have tried many things, and followed the guide on firebase documentation, but it just keeps running, but never loading.

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

}

推荐答案

您似乎在混淆两种类型的Cloud函数:

You seem to be confusing two types of Cloud functions:

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

通过常规HTTPS请求调用的云功能

将函数声明为functions.https.onRequest时,需要将响应写入响应对象.根据通过HTTP请求调用函数的文档, :

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

使用Firebase SDK调用的调用云函数

如果您想使用Firebase SDK在应用程序内调用您的Cloud Function ,您需要将函数声明为:

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

这篇关于无法使用Admin SDK Firebase获取用户列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆