M
M
Maria2018-08-14 13:39:14
Angular
Maria, 2018-08-14 13:39:14

How to get information from Firebase without subscribe?

I have such a situation that one Observable<User>is inside the other Observable<Params[]>, which is why I have to use Observable.subscribe nested in each other.
I need to get as an object one specific user from the database, whose id matches the route parameter.
Can you please tell me how to avoid this situation and get information from Firebase without a subscription? Because in fact, I do not need to subscribe to this user. I just need to get data about it to display on the form. Is this even possible?
If not, then how can nested subscriptions be avoided, because somewhere I came across the opinion that nested subscribes is not very good ...
Working with AngularFire 5

export class UserAddEditComponent implements OnInit {

  userForm: FormGroup;
  userId: string;

  constructor(private route: ActivatedRoute, private usersService: UsersService) { }

  ngOnInit() {
    this.route.params.subscribe((params: Params) => {
      if (params['id']) {
        this.userId = params['id'];
        this.usersService.getUser(this.userId).subscribe((user) => {
          this.initForm(user);
        });
      } else {
        this.initForm();
      }
    });
  }

The getUser()service method UsersServicereturnsObservable<User>
getUser (id: string): Observable<User> {
    return this.firebase.doc<User>(`users/${id}`).valueChanges();
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2018-08-14
@streetflush

https://angular.io/guide/router

ngOnInit() {
  this.hero$ = this.route.paramMap.pipe(
    switchMap((params: ParamMap) =>
      this.service.getHero(params.get('id')))
  );
}

Everything is extremely transparent

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question