How to return a Promise from an AngularFireObject using @ angular / fire in Angular (v6 +)

3
Hello, I'm working on an Angular project where I need to return a Promise on a given service. My code looks like this:

import { Injectable } from '@angular/core';
import { AngularFireDatabase } from '@angular/fire/database';

@Injectable({ providedIn: 'root' })
export class UserService {

  readonly baseUrl: string = 'user';

  constructor(
    private afDb: AngularFireDatabase
  ) { }

  object(id: string) {

    return this.afDb
      .object('${this.baseUrl}/{${id}}')
      .snapshotChanges()
      .toPromise()
  }

}

The problem is that I can not capture Promise by using the userService.object function inside the components. I'm doing this:

@Component({ selector: 'login', templateUrl: './login.html' })
export class Login {

  uid: 'teste'; 

  constructor(
    public userService: UserService
  ) { }

  login() {
    this.userService.object(this.uid)
      .then(res => console.log(res))
      .catch(err => console.error(err));
  }

}

What do I need to get my service to return a promise with the object coming from AngularFireDatabase ?

    
asked by anonymous 30.10.2018 / 14:43

0 answers