简体   繁体   English

Angular Firestore 加入集合不起作用

[英]Angular Firestore Join Collections not working

I have two collections that I am trying to join.我有两个想要加入的系列。 Review Comments and Users.查看评论和用户。

Here is the code in my service:这是我的服务中的代码:

 getAll(reviewID) {
    const allComments = this.af.collection<Review>(`reviews/${reviewID}/comments`, ref => ref.orderBy('dateCreated', 'desc'))
      .valueChanges()
      .pipe(
        switchMap(reviews => {
          const res = reviews.map((review: review) => {
            return this.af.collection<User>('users', ref => ref.where('userID', '==', review.userID))
            .valueChanges()
            .pipe(
              map(user => Object.assign(review, { user }))
            );
          });
          return combineLatest(...res);
        })
      );
      return allComments;
  }

Here is my component:这是我的组件:

this.commentService.getAll(reviewID).subscribe(results => {
        this.comments = results;
        this.logger.log(this.comments);
      });

This is mostly working but it seems to return the user array every other comment.这主要是有效的,但它似乎每隔一条评论就返回用户数组。

For example:例如:

0:
comment: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse id quam ut lacus tempor aliquam. Curabitur efficitur, lorem sit amet sodales finibus, erat arcu imperdiet dolor, eget ultricies arcu felis sit amet enim. Interdum et malesuada fames ac ante ipsum primis in faucibus."
dateCreated: Timestamp {seconds: 1549654515, nanoseconds: 0}
userID: "c8Y3q9Mn0sdfGFDgMWqh4GqJC5B2"
user: Array(0)
1:
comment: "this is the third comment"
dateCreated: Timestamp {seconds: 1549638673, nanoseconds: 0}
userID: "KEPBML9xlDfgedGZAslzOfOfbSwh2"
user: Array(1)
0: {avatar: "img.jpg", fullname: "Michael Jones", userID: "KEPBML9xlDfgedGZAslzOfOfbSwh2", username: "michaelj"}
2:
comment: "this is the second comment"
dateCreated: Timestamp {seconds: 1549562400, nanoseconds: 0}
userID: "NbPyKuTIB5dfGDGdVNmEB1h5YG2"
user: Array(0)
3:
comment: "this is the first comment"
dateCreated: Timestamp {seconds: 1549558800, nanoseconds: 0}
userID: "N2FhiRSJcdfGEDRgrCvPZa98bU4Im2"
user: Array(1)
0: {avatar: "img.jp", fullname: "Emily Jones", userID: "N2FhiRSJcdfGEDRgrCvPZa98bU4Im2", username: "emilyj"}

I have confirmed that all userIDs are being passed.我已确认所有用户 ID 都已通过。

Am I doing something wrong?难道我做错了什么? I'm not sure what I'm missing, any help would be appreciated.我不确定我错过了什么,任何帮助将不胜感激。

I finally figured it out.我终于弄明白了。 Not sure what I didn't think of this before.不确定我之前没有想到这一点。

To fix this I changed:为了解决这个问题,我改变了:

this.af.collection<User>('users', ref => ref.where('userID', '==', review.userID))

to

this.af.doc<User>( users/${review.userID} ) this.af.doc<User>(用户/${review.userID} )

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

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