Angular方法返回订阅者而不是bool

问题描述 投票:1回答:1

我需要有角度的帮助,我有一个方法可以验证电子邮件是否已经存在。

此代码负责:

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()))

这是谁调用方法:

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 }
}

值得注意的是,负责它的代码根据电子邮件的原始性返回true或false,但是调用此方法的人正在获得订阅者,我错在哪里?

回归的形象:Image of what its being returned

我将提供的任何其他信息。

编辑:我无法在链接副本中得到答案,我已经尝试了所有重复的内容,没有任何效果......

javascript angular typescript http
1个回答
0
投票

使用注释修改提到的结构

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
};
© www.soinside.com 2019 - 2024. All rights reserved.