javascript - 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?
解决方案
推荐阅读
- java - 使用 JAVA AdWords API 更新现有提要记录
- android - 使用 livedata 通过 refreshLayout 刷新数据
- sql - 从同一列创建两个别名,但根据内容赋予它们不同的值
- d3.js - d3 树 - 调整第一级父节点和子节点之间的水平距离
- javascript - 使用 React Hook 应用多个上下文的好方法
- python - 为什么会引发 NameError
- react-native - 如何取消日程通知
- javascript - 如何使用循环以表格形式打印 Localstorage 数组值?
- c# - 如何根据替换字符将一个字符串的值替换为另一个字符串?
- c# - 单击按钮时,我想将按钮文本发送到 viewModel (MVVM)