首页 > 解决方案 > FirebaseError:[code=invalid-argument]:函数 Query.where() 需要有效的第三个参数,但未定义

问题描述

我需要根据当前登录用户的uid查询一些数据。我有一个带有user$ observable的身份验证服务:

this.user$ = this.afAuth.authState.pipe(
  switchMap(user => {
    // logged in
    if (user){
      return this.afs.doc<User>(`users/${user.uid}`).valueChanges();
    }
    // logged out
    else {
      return of(null)
    }
  })

然后我有一个照片服务,它使用来自user$ observable的 uid 查询数据:

photos: Observable<Photo[]>;

uid;

constructor(public afs: AngularFirestore, private auth:AuthService) {
  this.auth.user$.subscribe(user => this.uid = user.uid);

  this.photos = this.afs.collection('photos',
  ref => ref.where('uid','==',this.uid).orderBy('createdAt','asc')).valueChanges();
}

getPhotos(){
  return this.photos;
}

最后,我有一个组件订阅可以从照片服务观察到的照片:

ngOnInit(){
  this.photoservice.getPhotos().subscribe(photos => this.photos = photos);
}

当我运行代码时,我收到错误:FirebaseError: Function Query.where() 需要有效的第三个参数,但它是未定义的。我认为这是因为查询在我获得 uid 之前运行。在运行查询之前如何获取 uid?

标签: javascriptangularfirebasegoogle-cloud-firestorefirebase-authentication

解决方案


推荐阅读