我在获取包含firebase列表和订阅方法的其他函数的值后尝试执行函数
我的代码中没有错误,但我不知道为什么调用函数没有返回值。
这是我的打字稿代码的一部分
来电者功能
sendChat(chatT){
if(this.checkPermission()){
let myDate: String = new Date().toISOString();
this.afd.list('/chats/').push({
uid:this.userService.id,
fname:this.userService.fname,
lname:this.userService.lname,
uemail:this.userService.email,
text:chatT,
time:myDate
}).then(_=>{
this.variables.chatText="";
}).then(_=>{
this.autoScroll();
})
}
}
返回功能
checkPermission(){
let check=false;
this.permissionsList.take(1).subscribe(dataX=>{
dataX.forEach(p=>{
if(p.uemail==this.userService.email && p.type=="chat" && p.status==false){
check=true;
let alert = this.alertCtrl.create({
title: "Sending Failed",
subTitle: "You are not allowed to send any chats.",
buttons: ['OK']
});
alert.present();
}
})
if(check==false){
return true;
}else{
return false;
}
})
}
注意:这些代码用于定义我的火力列表
public permissionsList:FirebaseListObservable;
...
this.permissionsList = this.afd.list(' / permissions /');
答案 0 :(得分:1)
我可以通过合并2个函数来解决问题,但是如果有任何人可以了解我如何等待返回的值将其用于更长的代码
sendChat(chatT){
let checkPer=false;
this.permissionsList.take(1).subscribe(dataX=>{
dataX.forEach(p=>{
if(p.uemail==this.userService.email && p.type=="chat" && p.status==false){
checkPer=true;
let alert = this.alertCtrl.create({
title: "Sending Failed",
subTitle: "You are not allowed to send any chats. for any comments or explanations, you can contact us",
buttons: ['OK']
});
alert.present();
}
})
if(checkPer==false){
let myDate: String = new Date().toISOString();
this.afd.list('/chats/').push({
uid:this.userService.id,
fname:this.userService.fname,
lname:this.userService.lname,
uemail:this.userService.email,
text:chatT,
time:myDate
}).then(_=>{
this.variables.chatText="";
}).then(_=>{
this.autoScroll();
})
}
})
}
答案 1 :(得分:1)
请查看以下代码。我不确定它是否能够正常工作,所以如果它出现任何错误请告诉我,以便我们解决它:
onclick
答案 2 :(得分:-1)
可能适合你吗
使用回叫功能来解决它
checkPermission(callback){
let check=false;
this.permissionsList.take(1).subscribe(dataX=>{
dataX.forEach(p=>{
if(p.uemail==this.userService.email && p.type=="chat" && p.status==false){
check=true;
let alert = this.alertCtrl.create({
title: "Sending Failed",
subTitle: "You are not allowed to send any chats.",
buttons: ['OK']
});
alert.present();
}
})
if(check==false){
callback(true);
}else{
callback(false);
}
})
}
然后
sendChat(chatT){
if(this.checkPermission(function(data){
let myDate: String = new Date().toISOString();
this.afd.list('/chats/').push({ uid:this.userService.id, fname:this.userService.fname, lname:this.userService.lname, uemail:this.userService.email, text:chatT, time:myDate }).then(_=>{
this.variables.chatText=""; }).then(_=>{ this.autoScroll(); }) } }