[英]Get request TypeError: this is undefined
我有两个功能:
这个检查用户名是否已经在数据库中使用:
public async checkIfExistUsername(){
this.authServiceProvider.getDataz("user/"+this.userData.username)
.then(data => {
if(data!=null) this.usernameTaken=1;
}, (err) => {
console.log("Couldn't check email");
});
}
这个检查电子邮件是否已经在数据库中使用:
public async checkIfExistEmail(){
this.authServiceProvider.getDataz("userbyemail/" + this.userData.email)
.then(data => {
if(data!=null) this.emailTaken=1;
}, (err) => {
console.log("Couldn't check email");
});
}
而且我有此功能,如果存在电子邮件/用户名,则会执行一些弹出窗口和提示
whatDo(){
if(this.usernameTaken!=0 && this.emailTaken!=0){
let alert = this.alertCtrl.create({
title: 'Erreur',
subTitle: 'Nom d\'Utilisateur & Email déjà utilisé par un autre Compte.',
buttons: ['ok']
});
alert.present();
}else if(this.usernameTaken!=0){
let alert = this.alertCtrl.create({
title: 'Erreur',
subTitle: 'Nom d\'Utilisateur déjà utilisé par un autre Compte.',
buttons: ['ok']
});
alert.present();
}else if(this.emailTaken!=0){
let alert = this.alertCtrl.create({
title: 'Erreur',
subTitle: 'Cet Email déjà utilisé par un autre Compte.',
buttons: ['ok']
});
alert.present();
}else{
this.showEtape1 = false;
this.showEtape2 = true;
this.showEtape3 = false;
this.showEtape4 = false;
}
}
这是AuthService提供程序中的getDataz函数:
public getDataz(path): Promise<any> {
return new Promise((resolve, reject) => {
this.http.get(apiUrl + path).subscribe((response) => {
resolve(response);
}, (err) => {
reject(err);
})
})
.catch((err) => {
throw err;
});
}
我这样调用这些树函数:
this.checkIfExistEmail()
.then(this.checkIfExistUsername)
.then(this.whatDo);
这是错误日志:
Error: Uncaught (in promise): TypeError: this is undefined
[59]/SignupPage.prototype.checkIfExistUsername/</<@http://localhost:8100/build/main.js:1388:17
step@http://localhost:8100/build/main.js:1249:18
verb/<@http://localhost:8100/build/main.js:1230:53
[59]/__awaiter</<@http://localhost:8100/build/main.js:1224:15
t@http://localhost:8100/build/polyfills.js:3:21506
[59]/__awaiter<@http://localhost:8100/build/main.js:1220:12
[59]/SignupPage.prototype.checkIfExistUsername@http://localhost:8100/build/main.js:1385:16
F</l</t.prototype.invoke@http://localhost:8100/build/polyfills.js:3:14974
onInvoke@http://localhost:8100/build/vendor.js:4982:24
F</l</t.prototype.invoke@http://localhost:8100/build/polyfills.js:3:14901
F</c</r.prototype.run@http://localhost:8100/build/polyfills.js:3:10124
f/<@http://localhost:8100/build/polyfills.js:3:20240
F</l</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:15649
onInvokeTask@http://localhost:8100/build/vendor.js:4973:24
F</l</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:15562
F</c</r.prototype.runTask@http://localhost:8100/build/polyfills.js:3:10815
o@http://localhost:8100/build/polyfills.js:3:7887
F</h</e.invokeTask@http://localhost:8100/build/polyfills.js:3:16823
p@http://localhost:8100/build/polyfills.js:2:27646
v@http://localhost:8100/build/polyfills.js:2:27893
Stack trace:
c@http://localhost:8100/build/polyfills.js:3:19752
c@http://localhost:8100/build/polyfills.js:3:19461
f/<@http://localhost:8100/build/polyfills.js:3:20233
F</l</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:15649
onInvokeTask@http://localhost:8100/build/vendor.js:4973:24
F</l</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:15562
F</c</r.prototype.runTask@http://localhost:8100/build/polyfills.js:3:10815
o@http://localhost:8100/build/polyfills.js:3:7887
F</h</e.invokeTask@http://localhost:8100/build/polyfills.js:3:16823
p@http://localhost:8100/build/polyfills.js:2:27646
v@http://localhost:8100/build/polyfills.js:2:27893
我试图将这些功能称为独立功能
this.checkIfExistEmail();
它运行良好,但我想运行checkIfExistEmail(异步),然后运行checkIfExistUser(异步),然后执行whatDo(同步)
将函数设置为回调时,需要使用bind或arrow函数。 this
值将不是预期的对象。
this.checkIfExistEmail()
.then(this.checkIfExistUsername.bind(this))
.then(this.whatDo.bind(this));
使用箭头,您可能还必须指定参数(如果有的话)。
this.checkIfExistEmail()
.then(()=>this.checkIfExistUsername())
.then(()=>this.whatDo());
如果您严格执行异步/等待。 您不需要像上面那样链接。
await this.checkIfExistEmail();
await this.checkIfExistUsername();
this.whatDo();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.