Reputation: 923
I'm trying to execute function after get the value of other function that contains firebase lists and subscribe method
no error in my code but I don't know why there is no value returned to the caller function.
This is a part of my typescript code
caller function
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();
})
}
}
returning function
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;
}
})
}
Note: these codes to define my firebase list
public permissionsList: FirebaseListObservable;
...
this.permissionsList = this.afd.list('/permissions/');
Upvotes: 1
Views: 1045
Reputation: 1
May it works for you
Use call back function to resolve it
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);
}
})
}
And then
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(); }) } }
Upvotes: -1
Reputation: 44659
Please take a look at the following code. I'm not sure if it'd work like it is, so please let me know if it throws any error so we can fix it:
public checkPermission(): Observable<any> {
let check=false;
return this.permissionsList
.take(1)
// Instead of subscribing to the observable, let's use the map
// operator, so we can subscribe to it later in the caller method
.map(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();
}
});
// I've simplified this part :)
return check === false;
});
}
sendChat(chatT){
// Call the method, and wait for the response
this.checkPermission().subscribe(
result => {
// Now we get the result, let's check it
if(result) {
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();
});
}
});
}
Upvotes: 1
Reputation: 923
I could solve the problem by merge the 2 functions, But wish if there is any one can learn me how can I wait the returned value to use it for the longer codes
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();
})
}
})
}
Upvotes: 1