【问题标题】:How to use WHERE function in Firebase using Angular?如何使用 Angular 在 Firebase 中使用 WHERE 函数?
【发布时间】:2021-08-13 22:44:49
【问题描述】:

实际目标:

  1. 用户可以在组织组中发帖,在该组织组中发布的帖子不能在其他组织组中可见,因此我尝试使用 Firebase 中的 WHERE 功能来过滤帖子列表。
  2. 用户发布的每个帖子都会在 Firestore 中保存组织组 ID(创建帖子的位置)。因此,当我过滤帖子列表时,我可以使用组织组 ID(postOrgId) 来过滤每个组织组中发布的特定帖子。

我想我在获取用户使用已激活路由查看的组织 ID 时遇到了问题。

图 1:组织 ID

我尝试以下方法。

  1. 我尝试使用激活路由,因为它会保存用户查看的当前组织组 ID,但不幸的是 Firebase 抛出错误。

  2. 我创建了一个获取当前组织 ID 的方法。

图 2:Firebase 抛出错误

这是组织组 (org-home.ts)

export class OrgHomePage implements OnInit, OnDestroy {

  loadOrganization: Organization;
  orgId: any;

  orgSub: Subscription;

  posts: Post[];
  isLoading = false;


  constructor(private orgService: OrganizationService,
    private afs: AngularFirestore,
    private activateRoute: ActivatedRoute,
    private authService: AuthService,
    public auth: AuthService,
    private storage: AngularFireStorage,
    private postService: PostService) {}//

  ngOnInit() {
    this.isLoading = true;

    //This will read the Organization Group ID
    this.orgId = this.activateRoute.snapshot.params['orgID'];

    // I call the method and passed the Org ID
    this.postService.getOrgId(this.orgId);

    this.orgSub = this.postService.getPosts().subscribe(posts => {
      this.posts = posts;
      this.isLoading = false;
    });

  }

以下是邮政服务 (post.service.ts)

export class PostService {
  postCol: AngularFirestoreCollection<Post>;
  postDoc: AngularFirestoreDocument<Post>;
  posts: Observable<Post[]>;
  post: Observable<Post>;

  post$: any;

  orgId: Observable<any>;

  constructor(
    private afs: AngularFirestore
    ) {


    this.postCol = this.afs.collection('post', ref => ref.orderBy("createdAt", "desc"));

    //Organization Group ID will be passed here to the WHERE Clause
    this.postCol = this.afs.collection('post', ref => ref.where("postOrgId", "==", this.orgId));

    this.posts = this.postCol.snapshotChanges().pipe(
      map(action => {
        return action.map(a => {
          const data = a.payload.doc.data() as Post;
          data.postId = a.payload.doc.id;
          return data;
        })
      })
    );
   }//

   //Gets all post
   getPosts() {
     return this.posts;
   }

   //It will get the Organization ID
   getOrgId(idParameter): Observable<any>{
    return this.orgId = this.afs.collection('post', result => result.where('postOrgId', '==', idParameter)).valueChanges();
   }

【问题讨论】:

  • 你能确定this.orgID 已定义吗/
  • 在我的帖子服务中,this.orgID 没有被读取,所以我想我在获取用户查看的当前组织 ID 时遇到了问题。

标签: angular typescript firebase ionic-framework google-cloud-firestore


【解决方案1】:

当我使用 Angular 和 Firebase 时,我会这样做:

constructor(private afs: AngularFirestore){}

someFunction(idParameter): Observable<any>{
 return this.afs.collection('Collection', result => result.where('fieldThatContainsTheId', '==', idParameter)).valueCahnges();
}

【讨论】:

  • 先生,我可以把这个放在我的邮政服务的什么地方?
  • 是的,放入您的 Post Service 并从您的组件中调用它。当我必须从 Firestore 集合中检索文档时,我就是这样做的。
  • 我试试你说的先生,但不幸的是,错误仍然存​​在。我将发布更新的代码。
猜你喜欢
  • 2020-08-27
  • 1970-01-01
  • 2021-01-25
  • 2018-11-15
  • 1970-01-01
  • 2020-05-07
  • 2017-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多