问题
How to use Firebase Admin SDK listUsers() function as an RXJS observable?
I can use from
RXJS function on listUsers
to return an observable, but the challenge is that listUsers
returns users in batches. How could these batches of users be retrieved and then merged?
回答1:
The Firebase Admin SDK function listUsers could be wrapped with a function that returns an Observable
.
Then flatMap
and forkJoin
could be used to combine the recursive function calls into one array of users.
import { forkJoin, from, of } from 'rxjs';
import { flatMap, map } from 'rxjs/operators';
function myListUsers(maxResults = 1000, pageToken = null) {
return from(admin.auth().listUsers(1000, pageToken));
}
function listAllUsers(nextPageToken) {
myListUsers(1000, nextPageToken).pipe(
flatMap(listUsersResult => forkJoin([
of(listUsersResult.users),
listUsersResult.pageToken ? listAllUsers(listUsersResult.pageToken) : of(null)
])),
map( ([userList1, userList2]) => [...userList1, ...userList2])
)
}
listAllUsers().subscribe(
allUsers => console.log(allUsers);
)
Note that the last array entry will be null
.
来源:https://stackoverflow.com/questions/58267346/firebase-admin-sdk-how-to-use-listusers-function-as-observable-combining-recu