0

In firebase, I am working on the sign up function. I signup the user (using email and password).

firebase.auth().createUserWithEmailAndPassword(email,password1).catch(function(error) {
    var errorCode = error.code;
    var error = document.getElementById("hiddenmessage");
    switch (errorCode) {
        case 'auth/email-already-in-use':
            error.innerHTML = "The account associated to "+email+" already exists. <a class=\"whitelink\"href=\"login.html\">Login here.</a>";
            error.style = "display: block;";
            break;
        //For future error code development
        default:
            error.innerHTML = errorMessage + "ERROR: <br>"+error.message+"<br>Try again later.";
            error.style = "display: block;";
    }
});

All of this is within another function, called the signUp() function. How do I return from both functions? I have already tried using break; and setting a variable.

Nobelium
  • 61
  • 8
  • `All of this is within another function` - well, that's the function that needs to know what to do with the result of the function you've shown – Jaromanda X Feb 08 '18 at 23:38
  • @JaromandaX What do you mean? – Nobelium Feb 08 '18 at 23:55
  • A function can not make a calling function do anything - all it does it return a value, it's up to the caller to then do whatever it needs to do\ – Jaromanda X Feb 08 '18 at 23:57
  • @Titus How do I change that .catch(function()) to a synchronous function then? – Nobelium Feb 08 '18 at 23:58
  • Take a look at the answers to the question I've mentioned, it contains some example of using `async/await`. – Titus Feb 09 '18 at 00:00
  • @JaromandaX I want it to return from the function with a value, like `return false;` and be able to read that from the parent function or return twice from both functions. – Nobelium Feb 09 '18 at 00:03
  • oh, in that case you need to read [How do I return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) as mentioned in the first comment – Jaromanda X Feb 09 '18 at 00:04
  • @Nobelium: since creating the user can take an undetermined amount of time, it happens asynchronously. Instead of making your code wait (and blocking the user's browser), the code continues and `createUserWithEmailAndPassword` returns. At this point the calling function can (for example) display a "please wait" message to the user. Then once creation is complete, your callback is called (of the `then()` or `catch()`). Since `createUserWithEmailAndPassword` has long since completed and returned, you can't return a value anymore. – Frank van Puffelen Feb 09 '18 at 04:50
  • You typically should move the code that requires the result into the `then()` or `catch()` handlers. For handling successful sign-in it is typically better to listen for `onAuthStateChanged()` as shown in the first snippet here: https://firebase.google.com/docs/auth/web/manage-users#get_the_currently_signed_in_user – Frank van Puffelen Feb 09 '18 at 04:51

0 Answers0