0

I am trying to retrieve the Firebase users in a project. I can retrieve them, but the main program never ends. I debug using VS Code or run the script with npm run and it gets stuck both ways. In VS Code there is nothing left on the stack...it just never stops

**Users function (It returns the users without a problem)

admin.auth().listUsers returns a listUsersResult object with properties nextPageToken and a users array

const BATCH_SIZE = 2;

const listAllUsers = async (nextPageToken = undefined) => {
  let listUsersResult = await admin.auth().listUsers(BATCH_SIZE, nextPageToken);

  if (listUsersResult.pageToken) {
    return listUsersResult.users.concat(await listAllUsers(listUsersResult.pageToken));
  } else {
    return listUsersResult.users;
  }
};

Main Routine (this is the one that gets stuck)

const uploadUsersMain = async () => {
  try {

    // if I comment out this call, then there is no problem
    let firestoreUsers = await listAllUsers();
  } catch(error) {
   log.error(`Unable to retrieve users ${error}`)
}
finally {
    // do stuff
  }
}

uploadUsersMain();

What could be the issue that stops the main program from ending? What should I look for? Thanks

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
aym
  • 233
  • 1
  • 4
  • 17
  • Never mind the recursion, how does it behave if you make just a single API request? https://stackoverflow.com/questions/37357080/exit-node-js-process-after-event-loop-is-empty-or-after-timeout – Josh Lee May 24 '19 at 01:37
  • Thanks Josh. I just limited the function to call admin.auth().listUsers(BATCH_SIZE, nextPageToken) and checked the result. I do get the desired values back but the main function keeps getting stucked. The returned value is an Object..no promise or anything. What is wrong? What can I check? It has to do definitely with the way I am calling the Promise, but I am a bit at lost on what to check. Thanks – aym May 24 '19 at 02:35

1 Answers1

2

To shut down your script you should use

await admin.app().delete();

Node.js will quietly stay running as long as there are handles, which can really be anything — a network socket certainly.

You could also use process.exit(), but it's good practice to properly shut down the SDK, instead of exiting abruptly when it might not be finished.

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • Thanks a lot Josh! That was the solution. Thanks for your help Firebase has all these small examples, but not a single *real* example :( – aym May 24 '19 at 19:05