1

I need help with angular, I have a method that makes the verification whether the email already exists or not.

This code is responsible for this:

emailExists = (email) => this.http.post(`${this.BASE_URL}/emailExists`, { 'email': email }).subscribe(response => {
    if (!response.json())
        return false

    this.handleError("Email already exists!")
    return true
}, error => this.handleError(error.json()))

This is who calls the method:

const emailValid = (auth) => 
control => {
    var a = auth.emailExists(control.value)
    var b = (/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/).test(control.value)

    if (!a && b)
        return null
    else
        return { invalidEmail: true }
}

It is noticeable that the code responsible for it returns true or false depending on the originality of the email, but who is calling this method is getting a Subscriber, where am I wrong?

Imagem do retorno: Image of what its being returned

Any additional information I will be making available.

EDIT: I couldn't get the answer in the linked duplicate, i've tried everything of that duplicate, nothing worked...

  • 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) – Heretic Monkey Feb 28 '18 at 16:00
  • My problem its not how to return the response, its why a Subscriber its the return... – Victor Hugo Schmidt Feb 28 '18 at 16:13
  • The why is that is what angular returns from an HTTP call. The fix is contained in the linked duplicate. – Heretic Monkey Feb 28 '18 at 16:13
  • i removed my answer because i misread your question. but the reason the subscribe is being returned is because your emailExists Method returns an observable and the reason that is happening is because Angulars HTTP Post method returns an observable. – Molik Miah Feb 28 '18 at 16:38
  • this should not return an observable and instead call the http.post method emailExists = (email) => {this.http.post(`${this.BASE_URL}/emailExists`, { 'email': email }).subscribe(response => { if (!response.json()) return false this.handleError("Email already exists!") return true }, error => this.handleError(error.json()))} – Molik Miah Feb 28 '18 at 16:43
  • i didn't understand the last comment – Victor Hugo Schmidt Feb 28 '18 at 16:45
  • @MolikMiah That still won't set `emailExists` to a Boolean value. – Heretic Monkey Feb 28 '18 at 16:47
  • 1
    @VictorHugoSchmidt You need to show how you applied the answers in the duplicate. It may be that you are misunderstanding how to apply them in your case. Basically, you will not be able to set `var a = auth.emailExists()` and have it be a Boolean. You have to do it asynchronously. – Heretic Monkey Feb 28 '18 at 16:49
  • Using async/await? Already done that, [link](https://i.imgur.com/N7UrehX.png) – Victor Hugo Schmidt Feb 28 '18 at 16:55

1 Answers1

0

Modify the structure as mentioned using comments

var emailExists = (email) => {    // Keep the rest of the code within this block
  let res: boolean;               // Response that you are expecting

    this.http.post(`${this.BASE_URL}/emailExists`, {
        'email': email
    }).subscribe(response => {
        if (!response.json()) {
            res = false;                           // Assign Value
            return;
        }

        this.handleError("Email already exists!")
        res = true;                                // Assign Value
        }), error => this.handleError(error.json());

    return res;                   // Return the response, instead of Subscriber
};
Abhijit Kar ツ
  • 1,732
  • 1
  • 11
  • 24