简体   繁体   English

Angularfire2 在 snapshotChanges() 中过滤数据

[英]Angularfire2 filter data inside snapshotChanges()

I have the classic function that get all booking in this way我有经典的 function 以这种方式获得所有预订

  getAll(startDate: Date, endDate: Date): Observable<Booking[]> {

  return this.db.collection(`hotel/${this.hoteluid}/booking`, ref =>

    ref.where('data_start', '<=', endDate)

  )
  .snapshotChanges().pipe(
    map((actions => actions.map(a => {
      const data = a.payload.doc.data() as Booking;
      const id = a.payload.doc.id;
      return { id, ...data };
    })))
  );
 }

when i try to filter data in this way当我尝试以这种方式过滤数据时

.snapshotChanges().pipe(
    map((actions => actions.map(a => {
      const data = a.payload.doc.data() as Booking;
      const id = a.payload.doc.id;
      if(data.num_room == 4)
      return { id, ...data };
    })))
  );

I obtain an error我得到一个错误

ERROR TypeError: Cannot read property 'uid_room' of undefined错误类型错误:无法读取未定义的属性“uid_room”

thanks all谢谢大家

In your map, if num_room is not 4, then it will be undefined no?在您的 map 中,如果 num_room 不是 4,那么它将是 undefined 不是吗?

Try a rather than use a conditional, I'd suggest a filter for the array in your map:尝试 a 而不是使用条件,我建议在您的 map 中对数组进行过滤器:

 .snapshotChanges().pipe(
    map(actions => 
      actions.map(a => {
        const data = a.payload.doc.data() as Booking;
        const id = a.payload.doc.id;
        return { id, ...data };
      }).filter(a =>
        a.num_room == 4
      )
    )
  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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