3
votes

I have a firebase setup application and need to get all documents in a collection before the related component render.

Data is coming through a normal subscription

this.db.getCountryList().subscribe(countryList => this.countryList = countryList); //working, I have the country list now

But when I use a resolver like below, it does not resolve data or nothing happens.

@Injectable()
export class CountryListResolver implements Resolve<Country[]> {

  constructor(private db: DatabaseService) { }

  resolve(): Observable<Country[]> {
    return this.db.getCountryList();
  }

}

service

getCountryList(): Observable<Country[]> {
    return this.db.collection<Country>(this.countryPath,ref => ref.orderBy('name', 'asc')).valueChanges();
  }

routing

{ path: 'geo', component: GeographicalDataComponent, resolve: {countryList: CountryListResolver } },

component

this.route.data.subscribe(data => {
       this.countryList = data['countryList'];
});

and registered my resolver in the root module and other non-firebase resolvers are working fine.

Why is this setup resolver not working?

version

firebase:4.11.0

angularfire2:^5.0.0-rc.6.0

angular:^5.2.0

2

2 Answers

0
votes

Try this:

CHANGE:

return this.db.getCountryList();

TO:

const doc = this.db.getCountryList().toPromise();
return doc.then(data => {
    return data;
});
0
votes

As myself had troubles to get this, and maked such question as you did, I tryied somethings after perceive that the request is not endend by the resolver, so the keypoint to solve this is just to complete the request with a workaround pipe as I exposed in my question

https://stackoverflow.com/a/64582997/3923628