2
votes

I'm trying to include a list of users (more than 50) through a specific function in Firebase. Here's my code:

Object.keys(newUsers).forEach((key) => {
    console.log(newUsers[key]['name']);
    admin.auth().createUser({
        uid: key,
        email: newUsers[key]['email']
        password: newUsers[key]['InitialPwd'],
        disabled: false,
        emailVerified: false,
        displayName: newUsers[key]['name'],
    }).then((userRecord) => {
        return console.log('Success');
    }).catch(function(error) {
        console.log("Error:", error);
    });                    
});

And the error is (for each record):

{ Error: Error while making request: timeout of 10000ms exceeded. at FirebaseAppError.FirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:39:28) at FirebaseAppError.PrefixedFirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:85:28) at new FirebaseAppError (/srv/node_modules/firebase-admin/lib/utils/error.js:119:28) at /srv/node_modules/firebase-admin/lib/utils/api-request.js:117:23 at at process._tickDomainCallback (internal/process/next_tick.js:228:7) errorInfo: { code: 'app/network-timeout', message: 'Error while making request: timeout of 10000ms exceeded.' }, codePrefix: 'app' }

How can I solve this?

1
Does this issue occur if you create just one user?Jen Person
Hi, @Jen, it doesn't occurs in one, or in a short list. In some tests, I detected that happens when I have more than 50 new usersLeandro Furini
Ah ok, that's good info. And are you running this within a Cloud Function?Jen Person
Yes @JenPerson. This Cloud Function starts when a user upload a CSV file to import new clients. It is necessary to create a user with an "Initial Password"Leandro Furini

1 Answers

1
votes

Cloud Functions are set to run for a short period of time. If you are doing lots of work in a Cloud Function, it may time out before it is complete. There are a few solutions to this that I would suggest:

1.Change your Cloud Functions timeout. In the Cloud console, check at the top to make sure your current project is selected, and then in the middle you'll find your list of functions. Click on your function. You should be in function details now. Click "Edit". Right above the "save" button is "more". Select "more" and you'll see an option for upping the timeout. This can modify how long the function stays alive.

2.Change the batch size so you're creating fewer users at a time.

3.Make sure your promises are working as expected. If you don't return the call to createUser, the resulting UserRecord won't be accessible.

Object.keys(newUsers).forEach((key) => {
    console.log(newUsers[key]['name']);
    return admin.auth().createUser({
        uid: key,
        email: newUsers[key]['email']
        password: newUsers[key]['InitialPwd'],
        disabled: false,
        emailVerified: false,
        displayName: newUsers[key]['name'],
     }).then((userRecord) => {
        return console.log('Success');
     }).catch(function(error) {
        console.log("Error:", error);
     });                    
  });

4.I may be incorrect about this point, but it appears that the users are created one after another rather than concurrently. This could be a good case to look into using Promise.all so that all of the users can be created simultaneously, rather than waiting for one to complete before starting the next.