1

I am learning Angular and trying to use Firestore through angular2-firestore. I created 2 collections structured as this:

    export interface Account {
      uid: string;
      title: string;
      startDate: Date;
      endDate: Date;
      manager: User;
      members: User[];
    }

    export interface User {
      uid: string;
      email: string;
      displayName: string;
      isActive: boolean;
    }

Now I need to query accounts by a user who is member in it. What I could do is fetch all of the accounts and then filter them using Typescript code, but I thought there should be some more efficient way to do this. Anybody please help. Thanks in advance.

Update

In my service, I added a method like this:

    @Injectable()
    export class AccountService {
    private static ACCOUNTS_TABLE_NAME = 'accounts';
    
      constructor(private db: AngularFirestore) {
      }
    
      public getAllOpenAccountsIAmIn(user: User) {
        return this.db.collection(AccountService.ACCOUNTS_TABLE_NAME, ref => {
          return ref.where('endDate', '==', null)
            // .where('members', 'contains', user); -- this is what I want something like
        }).valueChanges();
      }
    }

1 Answer 1

1

change your structure like this

export interface Account {
  uid {
    uid: string;
    title: string;
    startDate: Date;
    endDate: Date;
    manager: User;
    members: User[];
  }
}

export interface User {
  uid {
    uid: string;
    email: string;
    displayName: string;
    isActive: boolean;
  }
}

in your service

.where('uid.members', '==', user)

for reference watch this video till the end https://www.youtube.com/watch?v=IdKEhWAV-kE&t=3s

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.