0
votes

I am trying to create a new document with the id of the user's UID upon signup. However, when testing, nothing happens, not even an error code.

I think its an issue with "db.collection('users').doc("cred.user.uid").set(data);" and how the then function is called afterwards...

Very new to javascript & firebase here, (only 4 days in) so this shouldn't be too hard for you seasoned pros to figure out.

Thanks in advance!

Javascript

signupButton.addEventListener('click', signup);

function signup() {
    signupButton.style.display = 'none';
    signupError.style.display = 'none';

    // get user info
    var email = signupEmail.value;
    var password = signupPassword.value;

    // sign up the user
    firebase.auth().createUserWithEmailAndPassword(email, password).then(cred => {
        db.collection('users').doc("cred.user.uid").set(data);
        })
        .then(function() {
            window.location.replace('./dashboard/account.html');
        })
        .catch(function (error) {
            var errorCode = error.code;
            var errorMessage = error.message;
            console.log('Error code: ' + errorCode);
            console.log('Error message: ' + errorMessage);
            signupButton.style.display = 'flex';
            signupError.innerText = errorMessage;
            signupError.style.display = 'flex';
        });
}

    // Trigger button click on enter
    var input = document.getElementById("signupPasswordConfirm");

    // Execute a function when the user releases a key on the keyboard
    input.addEventListener("keyup", function(event) {
    // Number 13 is the "Enter" key on the keyboard
    if (event.keyCode === 13) {
        // Cancel the default action, if needed
        event.preventDefault();
        // Trigger the button element with a click
        document.getElementById("signupButton").click();
    }
});
1

1 Answers

0
votes

Seems like you'd want to return the promise from the Firestore operation so it could be picked up in the promise chain later to find out if there was an error:

firebase.auth().createUserWithEmailAndPassword(email, password).then(cred => {
    return db.collection('users').doc("cred.user.uid").set(data);
})

Note the return statement that propagates the promise returned by set().

Also, are you sure that you want to create a document called "cred.user.uid" literally? That's what you're doing here.