1

I am writing a function to generate a unique username for each new user. At first, assign a username matching with the full name of the user, then check that it exists in your database or not. If it exists then add some number to it and then check again until you come up with a new username that does not exist in the database.

Below is the code for the same

var generateUsername = function(name){
                var username = name;
                console.log(username);
                var isUsername;
                var number = 0;
                db.collection("users").where("username", "==", username)
                .get()
                .then(function(querySnapshot) {
                    if(querySnapshot.empty){
                        isUsername = false;
                    }
                    else {
                        isUsername = true;
                    }

                    while(isUsername == true){
                        number++;
                        username = fullName + number;
                        console.log(username);
                        db.collection("users").where("username", "==", username)
                        .get()
                        .then(function(querySnapshot) {
                            if(querySnapshot.empty){
                                isUsername = false;
                            }
                            else {
                                isUsername = true;
                            }
                        })
                        .catch(function(error) {
                            console.log("Error getting documents: ", error);
                        });
                    }
                })
                .catch(function(error) {
                    console.log("Error getting documents: ", error);
                });
            }

This is not giving me desired output. The loop is running infinitely. Whhere I am doing wrong?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Sachin Kumar
  • 366
  • 1
  • 6
  • 20
  • 2
    get() and all methods that return a promise are asynchronous and don't block the execution of code. Your while loop doesn't ever wait for the firestore query to complete - it's actually kicking lots of queries without waiting. – Doug Stevenson Apr 05 '20 at 15:15
  • so how to resolve this – Sachin Kumar Apr 05 '20 at 15:19
  • 2
    Does this answer your question? [While loops using Await Async.](https://stackoverflow.com/questions/39110762/while-loops-using-await-async) – andy mccullough Apr 05 '20 at 15:20

1 Answers1

0

So after andy mccullough suggestion, I came up with below solution

var generateUsername = function(name){
                var username = name;
                var isUsername;
                var number = 0;
                db.collection("users").where("username", "==", username).get()
                .then(function(querySnapshot) {
                    if(querySnapshot.empty){
                        isUsername = false;
                    }
                    else {
                        isUsername = true;
                    }
                    (async function loop(){
                        while(isUsername == true){
                            number++;
                            username = name + number;
                            var querySnapshot = await db.collection("users").where("username", "==", username).get();
                            if(querySnapshot.empty){
                                isUsername = false;
                            }
                            else {
                                isUsername = true;
                            }
                        }
                        console.log(username);
                    })();

                })
                .catch(function(error) {
                    console.log("Error getting documents: ", error);
                });
            }

It is necessary to wait loop function till database returns a promise. Using async await this can be acheived. You can refer to this answer for more details JavaScript ES6 promise for loop

Sachin Kumar
  • 366
  • 1
  • 6
  • 20