Firebase云函数报错TypeError: admin.listUsers is not a function及分页实现咨询
admin.listUsers is not a function & Implementing Pagination for Firebase Cloud Function Let's tackle your two questions one by one, with clear code examples and explanations:
1. Resolve the TypeError: admin.listUsers is not a function Error
The root cause here is a simple API misusage: user management methods in the Firebase Admin SDK live under the auth() module, not directly on the main admin instance. You're calling admin.listUsers() when you should be calling admin.auth().listUsers().
Here's the fixed version of your basic function (without pagination):
const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.getAllUsers = functions.https.onRequest((req, res) => { // Correct method call: admin.auth().listUsers() return admin.auth().listUsers() .then((userRecords) => { const users = userRecords.users.map(user => user.toJSON()); console.log('Retrieved all users'); // Return JSON response instead of plain text for better usability res.status(200).json({ success: true, users: users }); }) .catch((error) => { console.error('Request failed:', error); res.status(500).json({ success: false, error: error.message }); }); });
2. Implement Pagination for User Retrieval
Firebase's listUsers method natively supports pagination using two parameters:
limit: The maximum number of users to fetch per request (max 1000)pageToken: A cursor token returned from the previous request to fetch the next set of users
Note: Firebase doesn't support traditional page number-based pagination out of the box (since it doesn't track total user count efficiently). Instead, it uses cursor-based pagination with pageToken, which is more efficient for large datasets.
Updated Code with Pagination
This function will read limit and pageToken from the request query parameters, and return the users along with a nextPageToken for subsequent requests:
const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.getAllUsers = functions.https.onRequest((req, res) => { // Parse query parameters: set default limit to 100 if not provided const limit = parseInt(req.query.limit) || 100; const pageToken = req.query.pageToken || undefined; // Validate limit (Firebase allows max 1000 users per request) if (limit < 1 || limit > 1000) { return res.status(400).json({ success: false, error: 'Limit must be between 1 and 1000' }); } return admin.auth().listUsers(limit, pageToken) .then((userRecords) => { const users = userRecords.users.map(user => user.toJSON()); res.status(200).json({ success: true, users: users, nextPageToken: userRecords.pageToken // Will be null if no more users }); }) .catch((error) => { console.error('Request failed:', error); res.status(500).json({ success: false, error: error.message }); }); });
How to Call the Function with Pagination
First Page (no pageToken needed):
curl "http://localhost:5000/sweltering-fire-5301/us-central1/getAllUsers?limit=5"
This will return 5 users and a nextPageToken (if there are more users).
Subsequent Pages (use the nextPageToken from the previous response):
curl "http://localhost:5000/sweltering-fire-5301/us-central1/getAllUsers?limit=5&pageToken=YOUR_NEXT_PAGE_TOKEN"
Optional: Supporting page Parameter (Not Recommended)
If you strictly need to use a page number instead of pageToken, you'll have to iterate through pages until you reach the desired one. This is inefficient for large datasets, but here's a basic example:
// Warning: This is inefficient for large user bases! exports.getAllUsersWithPage = functions.https.onRequest((req, res) => { const limit = parseInt(req.query.limit) || 100; const page = parseInt(req.query.page) || 1; let currentPage = 1; let pageToken = undefined; let targetUsers = []; const fetchPage = () => { return admin.auth().listUsers(limit, pageToken) .then((userRecords) => { if (currentPage === page) { targetUsers = userRecords.users.map(user => user.toJSON()); return { users: targetUsers, nextPageToken: userRecords.pageToken }; } else if (userRecords.pageToken) { currentPage++; pageToken = userRecords.pageToken; return fetchPage(); } else { // No more pages, return empty array if page is out of bounds return { users: [], nextPageToken: null }; } }); }; return fetchPage() .then((result) => { res.status(200).json({ success: true, users: result.users, nextPageToken: result.nextPageToken }); }) .catch((error) => { console.error('Request failed:', error); res.status(500).json({ success: false, error: error.message }); }); });
Call it with:
curl "http://localhost:5000/sweltering-fire-5301/us-central1/getAllUsersWithPage?limit=5&page=2"
内容的提问来源于stack exchange,提问作者methuselah

