I need to know whether this data entry is available or not. So I need to handle promise to return boolean. I tried various ways but it didn't work. I use the below code to do the task, I need to return promise as boolean.
//constants for the database data lists
readonly USER_CHILD_ROOT: string = "user";
list: AngularFireList<any>;
private newUser: DatabaseUser = {
uid: "",
email: "",
name: "",
address01: "",
address02: "",
address03: "",
telephone: "",
}
fileList: any[];
constructor(@Inject(AngularFireDatabase) private firebase: AngularFireDatabase) { }
isUserAvailable(userEmail: string): boolean {
var output: boolean = false;
this.firebase.list(this.USER_CHILD_ROOT)
.snapshotChanges()
.subscribe(
list => {
this.fileList = list.map(item => { return item.payload.key; });
this.fileList.forEach(element => {
if (element === userEmail)
output = true;
//console.log(element);
});
});
return output;
}
This modified solution worked.
async isUserAvailable(userEmail: string) {
return await new Promise(resolve => {
this.firebase.list(this.USER_CHILD_ROOT)
.snapshotChanges()
.subscribe(
list => {
this.fileList = list.map(item => { return item.payload.key; });
this.fileList.forEach(element => {
if (element === userEmail) {
resolve(true);
} else {
resolve(false);
}
});
});
});
}
Call and wait for the response.
let promise = this.fdd.isUserAvailable("user email");
promise.then(
function(result){
if(result)console.log("found!")
}
);