简体   繁体   中英

Using conditional statements with Angularfire and rxjs Observable operators

All

I've been searching for a solution to problem for about a week and can't find any answsers, so hoping someone here can point out what I am doing wrong. I have a FirebaseList observable that I use with Firebase that uses .where() to filter as search function in the first instance.

Once this completes I am trying to use an If statement to determine if the result is still valid (as Firebase cannot perform all search functions) and then if the result is still valid, insert another object which contains user information (another Observable).

I have tried using a combination of subscribe/map/flatMap and switchMap but cannot return just the Observable stream. The results either return as multiple arrays, returns 'undefined' in the stream or other undesired.

I'm not sure on how to get this working with Plunker so have copied the code below. I also need to return the objects with keys hence the additional payload step.

return ths.afs.collection('results', ref=> ref.where('location', '==', 
searchLocation.where('date', '==', searchDate)
.snapshotChanges().map(actions => {
 return actions.map(a => {
  const data = a.payload.doc.dat();
  const key = a.payload.doc.key;
  return{ key, ...data};
}).map(trueResults => {
 if (!trueResults.removedid.includeds(firebaseUser.uid || trueResults.status === 'available'){
   this.userService.getUserObj(trueResults.ownerId).subscribe(userObj => {
    trueResults.userOwnerObj = userObj;     
   });
 }
 return trueResults
}

In my component is where I subscribe to the results for display on the search page.

I have also tried with pushing the trueResult onto an Array, but worried that will break the Observable nature of this function.

Any advise appreciated!

I went back to an earlier setup and for some reason worked. It involved mapping the results after the payload and pushing onto an array. For example

return ths.afs.collection('results', ref=> ref.where('location', '==', 
searchLocation.where('date', '==', searchDate)
.snapshotChanges().map(actions => {
 return actions.map(a => {
  const data = a.payload.doc.dat();
  const key = a.payload.doc.key;
  return{ key, ...data};
});
}).map(trueResults => {
trueResults.forEach(trueResult => {
 if (!trueResult.removedid.includeds(firebaseUser.uid || trueResult.status 
=== 'available'){
   this.userService.getUserObj(trueResults.ownerId).subscribe(userObj => {
    trueResult.userOwnerObj = userObj;     
    arrayOfTrueResults.push(trueResult)
   });
})
return arrayOfTrueResults;
 })
}

Hope this helps anybody in the same situation. Note sure above subscribing to the other variable inside here but will revisit later if there is a better alternative, I am still learning

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM