'Firebase admin: list all users returns empty array
I have an https-callable firebase cloud function to return all users as an array of their userRecords. I push each of the userRecords into an array which I later return, but it returns an empty array.
How can I return this array to the client?
index.js:
exports.listAllUsers = functions.https.onCall((data, context) => {
// List all users
return listAllUsers();
});
function listAllUsers(nextPageToken) {
// List batch of users, 1000 at a time.
var allUsers = [];
return admin.auth().listUsers(1000, nextPageToken)
.then(function (listUsersResult) {
listUsersResult.users.forEach(function (userRecord) {
// For each user
var userData = userRecord.toJSON();
allUsers.push(userData);
});
if (listUsersResult.pageToken) {
// List next batch of users.
console.log('Next batch of users');
return listAllUsers(listUsersResult.pageToken)
}
else {
// All users have been fetched
console.log('All users have been fetched');
return allUsers
}
})
.catch(function (error) {
console.log("Error listing users:", error);
});
}
Client side
const listAllUsers = this.afFunctions.httpsCallable('listAllUsers');
listAllUsers({}).toPromise()
.then(function (result) {
console.log(result);
});
}
Solution 1:[1]
It might be a shot in the dark, but for me it was the fact that I had FIREBASE_AUTH_EMULATOR_HOST
environment variable set. Once I commented it out, and restarted my IDE, it started working again.
Whatever your scenario, best is probably to look at your initializeApp
function and to verify what goes into it, plus ensure the emulator (and the env variable for the auth host - as mentioned above) is off while you're running the script.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | benomatis |