Angular FIrebase 5 objects keys not being displayed. So can't delete

后端 未结 2 1067
说谎
说谎 2020-12-03 23:05

The question has been answered but I\'m looking for a, um, more straightforward one if available. It seems strange that we\'d have to implement not one but two mappings just

2条回答
  •  独厮守ぢ
    2020-12-04 00:06

    Updated Answer

    Rxjs have changed how it pipes data. now you have to use .pipe().

    this.courses$ = this.courses.snapshotChanges().pipe(
      map(changes => 
        changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
      )
    );
    

    Original Answer

    .valueChanges() contain simply data, no key with it. you need to use .snapshotChanges()

    this.courses$ = this.courses.snapshotChanges().map(changes => {
      return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
    });
    

    now just use {{course.key}}

    here is your corrected code

    import { Observable } from 'rxjs/Observable';
    import 'rxjs/add/operator/map';
    
    export class AppComponent {
      courseRef: AngularFireList;
      courses$: Observable;
    
      constructor(private db: AngularFireDatabase) {
        this.courseRef = db.list('/courses');
        this.courses$ = this.courseRef.snapshotChanges().map(changes => {
            return changes.map(c => ({ key: c.payload.key, ...c.payload.val() 
        }));
       });
      }
      ...
      deleteCourse(course) {
        console.log(course.key);
        this.db.object('/courses/' + course.key).remove();
      }
    }
    

提交回复
热议问题