Angular 获取请求类型错误:这是未定义的
我有两个职能: 此项检查数据库中是否已使用用户名:Angular 获取请求类型错误:这是未定义的,angular,typescript,ionic-framework,ionic3,Angular,Typescript,Ionic Framework,Ionic3,我有两个职能: 此项检查数据库中是否已使用用户名: public async checkIfExistUsername(){ this.authServiceProvider.getDataz("user/"+this.userData.username) .then(data => { if(data!=null) this.usernameTaken=1; }, (err) => { console.log("Couldn't
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");
});
}
此项检查电子邮件是否已在数据库中使用:
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 provider中的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;
});
}
以下是错误日志:
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
它工作得很好,但我想先运行checkIfExistEmail(异步),然后运行checkIfExistUser(异步),然后运行whatDo(同步)在将函数设置为回调时,需要使用bind或arrow函数。
此
值将不是预期的对象
this.checkIfExistEmail()
.then(this.checkIfExistUsername.bind(this))
.then(this.whatDo.bind(this));
使用箭头,您可能还必须指定参数(如果有)
如果您正在执行严格的异步/等待。你不需要像上面那样连锁
await this.checkIfExistEmail();
await this.checkIfExistUsername();
this.whatDo();
哥们,它的工作原理,已经被困在这几个星期了哈哈,我对爱奥尼亚和整个异步和同步功能相当陌生,你能给我解释一下它是如何工作的吗?这不是爱奥尼亚特有的。。与promises和javascript更相关。在这里和arrow Yea ikr,我读了一些关于promises如何工作的文章,但文章没有说明如何让应用程序等待异步函数,你的解决方案和我的解决方案有什么区别,绑定做了什么?为什么在使用bind时必须使用参数?bind将回调的
this
值设置为调用函数的this
,而不是调用位置。您必须在arrow not bind中指定参数。这与异步/等待无关。
await this.checkIfExistEmail();
await this.checkIfExistUsername();
this.whatDo();