vendredi 26 février 2021

If firestore doc.exists how do I stop execution of parent function?

I'm trying to stop the async function if a username is found in the database. I can find existingUsername = true, but handleSignup continues to execute and overwrites existing user data.

async function handleSignup {

    var docRef = db.collection("users").doc(username);
    var existingUsername = false

    docRef.get().then((doc) => {
        if (doc.exists) {
            existingUsername = true
        }
        else {
            existingUsername = false
        }
    })

    if (existingUsername == true) {
        return setError("Username exists") // How do I stop the function handleSignup?
    }

    }
//database code to create user
}

Aucun commentaire:

Enregistrer un commentaire