简体   繁体   English

Angular 在启动其他 Observable 之前等待一个 Observable 完成

[英]Angular Wait an Observable to finish before starting other Observable

I have two Observables :我有两个 Observables :

constructor(
    private afAuth:AngularFireAuth,
    private db:AngularFireDatabase
    ) {
      
    this.afAuth.authState.subscribe(x=> 
      {
        this.userId=x?.uid;
        console.log(this.userId);
      });
      
      console.log(this.userId);
      this.db.list('users/'+this.userId+'/movies-favourited').valueChanges().subscribe(data=>{
        this.favouriteList=data;
      });
   }

How do I make sure that I get the userId in the first Observable before getting the list in second Observable.?在第二个 Observable 中获取列表之前,如何确保在第一个 Observable 中获取 userId。

The correct way to do it is by using switchMap as shown below:正确的做法是使用switchMap ,如下所示:

this.afAuth.authState.pipe(
    switchMap(user => {
        if(user) {
            this.userId = user.uid;
            this.db.list('users/'+this.userId+'/movies-favourited')
                .valueChanges().subscribe(data => {
                    this.favouriteList = data;
                });
        }
    })
);

You can read more about switchMap here .您可以在此处阅读有关switchMap更多信息。

You can either replace the first observable with promise您可以用 promise 替换第一个 observable

OR或者

You can put the second observable inside the first one like this:您可以像这样将第二个 observable 放在第一个 observable 中:

this.afAuth.authState.subscribe(x=> 
  {
    this.userId=x?.uid;
    console.log(this.userId);
  this.db.list('users/'+this.userId+'/movies-favourited').valueChanges().subscribe(data=>{
    this.favouriteList=data;
    });
  });

Once the first observable successfully retrieve data by subscribing it will subscribe to the second observable which will solve your problem.一旦第一个 observable 通过订阅成功检索数据,它将订阅第二个 observable,这将解决您的问题。

constructor(
    private afAuth: AngularFireAuth,
    private db: AngularFireDatabase
) {
    this.afAuth.authState.subscribe(x => {
        this.userId = x?.uid;
        console.log(this.userId);
        this.db.list('users/' + this.userId + '/movies-favourited').valueChanges().subscribe(data => {
            this.favouriteList = data;
        });
        console.log(this.userId);
    });
}

This must be the solution for you task.这必须是您任务的解决方案。

this.afAuth.authState
                .subscribe(
                    (x: any) => {
                        this.userId = x?.uid;
                        if (x.uid) {
                            this.db.list('users/' + this.userId + '/movies-favourited').valueChanges()
                                .subscribe(
                                    (data: any) => {
                                        this.favouriteList = data;
                                    },
                                    (error) => {
                                        console.log(error);
                                    });
                        }
    
                    },
                    (error) => {
                        console.log(error);
                    }
                );

More suggestions to optimize your code:更多优化代码的建议:

  1. Consider put your code inside ngOnInit() lifecycle instead of constuctor.考虑将您的代码放在 ngOnInit() 生命周期中而不是构造函数中。
  2. Define data types of your variables 'x' and 'data'.定义变量“x”和“data”的数据类型。
  3. Don't forget to unsubscribe.不要忘记取消订阅。
  4. Handle your errors in some way.以某种方式处理你的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM