gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:30 32 4
gpt4 key购买 nike

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

这段代码负责:

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,但是调用此方法的人正在获取订阅者,我哪里错了?

Imagem 返回: Image of what its being returned

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

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

最佳答案

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

关于javascript - Angular 方法返回订阅者而不是 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49033612/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com