最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you list all users in firebase firestore function - Stack Overflow

programmeradmin3浏览0评论

I have been learning firebase firestore functions and I wanted to list all users and have then pare a value to that they have stored. Such as if there are 20 users and I want to see all users named "mike" how can I get an array of the users so I can pare them so I can find all users name "mike"?

I am running:

"firebase-admin": "^5.11.0",
"firebase-functions": "^1.0.0"

I saw this snippet of code for firebase admin but I dont thing it works as it wasn't labeled a firestore function, but if it does if I return the "admin.auth().listUsers..." will I get an arraylist of users?

    function listAllUsers(nextPageToken) {
  // List batch of users, 1000 at a time.
  admin.auth().listUsers(1000, nextPageToken)
    .then(function(listUsersResult) {
      listUsersResult.users.forEach(function(userRecord) {
        console.log("user", userRecord.toJSON());
      });
      if (listUsersResult.pageToken) {
        // List next batch of users.
        listAllUsers(listUsersResult.pageToken)
      }
    })
    .catch(function(error) {
      console.log("Error listing users:", error);
    });
}

I have been learning firebase firestore functions and I wanted to list all users and have then pare a value to that they have stored. Such as if there are 20 users and I want to see all users named "mike" how can I get an array of the users so I can pare them so I can find all users name "mike"?

I am running:

"firebase-admin": "^5.11.0",
"firebase-functions": "^1.0.0"

I saw this snippet of code for firebase admin but I dont thing it works as it wasn't labeled a firestore function, but if it does if I return the "admin.auth().listUsers..." will I get an arraylist of users?

    function listAllUsers(nextPageToken) {
  // List batch of users, 1000 at a time.
  admin.auth().listUsers(1000, nextPageToken)
    .then(function(listUsersResult) {
      listUsersResult.users.forEach(function(userRecord) {
        console.log("user", userRecord.toJSON());
      });
      if (listUsersResult.pageToken) {
        // List next batch of users.
        listAllUsers(listUsersResult.pageToken)
      }
    })
    .catch(function(error) {
      console.log("Error listing users:", error);
    });
}
Share Improve this question asked Apr 8, 2018 at 1:31 BhagyeshBhagyesh 7001 gold badge11 silver badges36 bronze badges 3
  • The code you show (from this documentation section for those interested) shows precisely how to get an array of users from Firebase Authentication. It does however have nothing to do with Cloud Firestore, nor does it have anything to do with Cloud Functions for Firebase. If you want to get a list of users, I suggest you first try to run this code in a local Node.js script and then work up from there. – Frank van Puffelen Commented Apr 8, 2018 at 2:24
  • @FrankvanPuffelen but how would i get that to connect to the functions in the cloud – Bhagyesh Commented Apr 8, 2018 at 2:35
  • I strongly remend taking it one step at a time. First get it working on a local Node.js system. Once you have that working and you're ready to move it to Cloud Functions so you can call it from you app, have a look here: firebase.google./docs/functions/callable – Frank van Puffelen Commented Apr 8, 2018 at 3:24
Add a ment  | 

2 Answers 2

Reset to default 1

This is how I done it:

const functions = require('firebase-functions');
const admin = require('firebase-admin')

admin.initializeApp()

const express = require('express')
const app = express()

app.get('/users', async (req, res) => {
    try {
        const listUsers = await admin.auth().listUsers()

        return res.status(200).send({ listUsers })
    } catch (err) {
        return handleError(res, err)
    }
})

exports.api = functions.https.onRequest(app)

this shows a list of users but still need some work to make it better

Update

    exports.getAllUsers = async (req, res) => {
    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);
        });
}

Maybe you should do that search with the function getUserByDisplayName(). You can see it in this Firebase guide: https://firebase.google./docs/auth/admin/manage-users#list_all_users. But as Frank said that has nothing to do with firebase-functions. It is a part of Firebase Auth.

Hope this helps!

发布评论

评论列表(0)

  1. 暂无评论