Ad

TypeError: Admin.listUsers Is Not A Function

I have deployed the following cloud function:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.getAllUsers = functions.https.onRequest((req: any, res: any) => {
  return admin.listUsers().then((userRecords: any) => {
    userRecords.users.forEach((user: any) => console.log(user.toJSON()));
    res.end('Retrieved all users successfully.');
  }).catch((error: any) => console.log('Request failed: ', error));
})

But whenever I try to run it, I get the following error:

TypeError: admin.listUsers is not a function
    at exports.getAllUsers.functions.https.onRequest (/srv/functions/lib/index.js:102:11)
    at cloudFunction (/srv/functions/node_modules/firebase-functions/lib/providers/https.js:57:9)
    at process.nextTick (/srv/node_modules/@google-cloud/functions-framework/build/src/invoker.js:243:17)
    at process._tickCallback (internal/process/next_tick.js:61:11)

How do I resolve it? And how do I add a parameter to paginate my data, i.e. to get 100 users at a time or something? I am expecting to call the endpoint as follows: curl http://localhost:5000/sweltering-fire-5301/us-central1/getAllUsers. How would it look like with a parameter i.e. 'http://localhost:5000/sweltering-fire-5301/us-central1/getAllUsers?limit=5&page=2'

Ad

Answer

If you imported the admin SDK like this:

const admin = require("firebase-admin")

Then listUsers is available to you like this:

admin.auth().listUsers()

You might want to check out the documentation and API reference, as they have everything you need to make this happen.

Ad
source: stackoverflow.com
Ad