Search code examples
angularfirebasefirebase-realtime-databaseangularfire

get single record data AngularFireDatabase


Below is my screenshot database:

enter image description here

I want to get single course - by id, here is my code:

Service:

 getSingle(uid: string) {
    return this.db
      .object(`courses/${uid}`)
      .snapshotChanges()
      .pipe(map(res => res.payload.val()));
  }

Component:

ngOnInit() {
    this.coursesService.getSingle('Kyric46r714Kibjqw2E').subscribe(item => {
      console.log(item);
    });

i am getting null - in console.log(item)

Moreover, if i change my service to this:

 getSingle(uid: string) {
    return this.db
      .object(`courses/${uid}`)
      .snapshotChanges()
      .pipe(map(res => res.payload.key)); < ------------
  }

i am getting in console.log(item) my key - Kyric46r714Kibjqw2E, as i should.

why the val() method doesn't work?

Thanks for help in advance!!!


Solution

  • You need to add the dash - next to you id to retrieve the data inside of it:

    this.coursesService.getSingle('-Kyric46r714Kibjqw2E').subscribe(item