Is there a way to transform this function into an observable? I need it to check for the existence of a document based on a query, and I want to be able to subscribe to it in order to create a new document if one does not already exist.
Unfortunately, I am encountering an error:
A function that is declared with a type other than 'void' or 'any' must return a value.
exists(query: Vehicle): Observable<boolean>{
return new Observable(observer => {
this.afs.collection('vehicles',
ref =>
ref
//queries
.where("country","==", query.country)
).snapshotChanges().subscribe(
res => {
if (res.length > 0){
observer.next(true);
observer.complete();
}
else {
observer.next(false);
observer.complete();
}
});
});
}//end exists()
After making it an observable, you can call it like this:
this.vehicleService.exists({country:"USA"})
.subscribe(x => {
if (x) {
//create a new doc
}
});