3

I'm trying to add User data into Cloud Firestore after using Firestore Auth when a new user is created.

$("#signup_btn").click(function () {
    var firstname = document.getElementById("firstname").value;
    var lastname = document.getElementById("lastname").value;
    var email = document.getElementById("email_signup").value;
    var password = document.getElementById("password_signup").value;

    firebase.auth().createUserWithEmailAndPassword(email, password).then(cred=>{
        this.createNewUserData(firstname,lastname,email);
    }).catch(error =>{
        var user = firebase.auth().currentUser;

        user.delete();
        var errorMessage = error.message;

        window.alert("Error: "+ errorMessage);
    });
});


function createNewUserData(firstname, lastname, email){
    db.collection("users").add({
        firstname: firstname,
        lastname: lastname,
        email: email
    })
        .then(function (docRef) {
            console.log("Document written with ID: ", docRef.id);
        })
        .catch(function (error) {
            console.error("Error adding document: ", error);
        });
}

The above code I've implemented is able to add user data into the firebase Auth however it doesn't add any data into the firestore database. Is there a better way to implement this function?

1 Answer 1

1

You cannot call another function using this.createNewUserData. Thus, you need to call createNewUserData(firstname,lastname,email); from the then block of createUserWithEmailAndPassword.

Currently,the reason that the it does not add any user data into the firestore database is that the function createNewUserData is not called. So, once that function is called you will get at least a call to firestore to add that user.

Sign up to request clarification or add additional context in comments.

1 Comment

Its the same whether i put this or without. it still doesn't add the data into firestore

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.