本文介绍了无法获取使用 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);
    });

}

推荐答案

你好像混淆了两种云函数:

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 从您的应用中调用您的云函数,你需要将你的函数声明为:

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 的用户列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 08:05