0

I have a function in my ionic app to check if user email is already exists in firebase database.

  checkValidEmail(email):any{
    let result=this.userService.getUserByEmail(email);
    console.log(result);
    result.subscribe((k:User[])=>{
      if(k.length>0){
        return true;
      }else{
        return false;
      }
    });
  }

I am trying to pass its boolen result in following if condition to check whether entered email address is already exists in database. It it exists display an error.

if(this.checkValidEmail(this.user.email)){
      console.log("Error : Already has this email address ");
      this.error ="Already has this email address in out system";
    }

But I cant get true or false into if(this.checkValidEmail(this.user.email)) . Please help I'm new in this.

  • 1
    I'm not familiar with the libraries being used here, but `checkValidEmail` looks asynchronous. If that's the case, you'll need to return a promise of a value, and add a completion callback. – Carcigenicate May 02 '18 at 04:48
  • Possible duplicate of [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) – Carcigenicate May 02 '18 at 04:49
  • I recommended that dupe because I'm pretty sure that this is the case. – Carcigenicate May 02 '18 at 04:49
  • can you please give more detail. I am new in this. – Ranjit Singh Shekhawat May 02 '18 at 04:50
  • Read the answers in the link I posted. Basically, the problem is that `checkValidEmail` returns before `subscribe` has a chance to complete. You're also returning from the function that you gave to `subscribe`, not `checkValidEmail`. You'll need to get a good understanding of how to deal with asynchronous code. – Carcigenicate May 02 '18 at 04:52
  • The specified return type of the function is "any", you must change it to Boolean. Changing it will throw an error, solve that and you're done. – Prasheel May 02 '18 at 04:53
  • 1
    @Prasheel That's wrong, even if `subscribe` is synchronous. They never return anything from `checkValidEmail`. – Carcigenicate May 02 '18 at 04:55
  • Included an answer with simple callbacks, there are promises and observable approaches you could use to handle async streams but for simplicilty I have included a callback based implementation. – Lasitha Petthawadu May 02 '18 at 04:56
  • @Carcigenicate, I missed that those return statements are enclosed in subscribe. – Prasheel May 02 '18 at 04:57
  • @Prasheel I changed its to Boolean as you suggested. It shows this error : `A function whose declared type is neither 'void' nor 'any' must return a value.` `checkValidEmail(email):boolean {` – Ranjit Singh Shekhawat May 02 '18 at 05:00
  • @RanjitSinghShekhawat Check my answer its not needed to have a return type for checkValidEmail since its subscribe that returns a value. – Lasitha Petthawadu May 02 '18 at 05:05
  • this is the error @Carcigenicate was talking about. Your function is not returning anything, the return statements are enclosed under subscribe(). – Prasheel May 02 '18 at 05:07

1 Answers1

1

The result object is an asynchronous stream and the subscribe callback triggers at a later time so checkValidEmail doesn't return a result. You can use a callback and get that callback to trigger or you could use promises or RxJS observables.

To keep things simple I have changed your code with a simple callback function.

  checkValidEmail(email,resultCallback){
    let result=this.userService.getUserByEmail(email);
    console.log(result);
    result.subscribe((k:User[])=>{
      if(k.length>0){
        resultCallback(true);
      }else{
        resultCallback(false);
      }
    });
  }

Revise the code as follows

    this.checkValidEmail(this.user.email,(isError)=>{
        if (isError){
          console.log("Error : Already has this email address ");
          this.error ="Already has this email address in out system";
        }


});