Firestore where statement with array-containts doesn't work











up vote
0
down vote

favorite












I check my database for a reference (which exists) and i don't get any chats back but I don't see where.



chat.service.ts:



  getUserChats(): Observable<Chat> {
let uid = '-1';
this.auth.currUser.subscribe(user => (uid = user.uid));
this.chats = this.afs
.collection('chats', ref => ref.where('members', 'array-contains', `users/${uid}`))
.snapshotChanges()
.pipe(
map(actions => {
return actions.map(action => {
const data = action.payload.doc.data() as Chat;
const id = action.payload.doc.id;
return {id, ...data};
});
})
) as Observable<Chat>;
return this.chats;
}


The code is intented to get all the chats where the logged in user is a member in. I do this by checking the members array, if there is any reference to him.



chat.component.ts:



chats: Observable<Chat>;
constructor(private router: Router, public chatService: ChatService, public dialog: MatDialog, public auth: AuthService) { }

ngOnInit() {
this.chats = this.chatService.getUserChats();
}


chat.component.html:



<mat-list-option *ngFor="let chat of chats | async" (click)="handleClick(chatList)" [value]="chat.id">
<img matListAvatar src="./assets/avatar.svg" alt="Avatar Icon">
<h3 mat-line>{{chat.name}}</h3>
<p mat-line>test</p>
</mat-list-option>


Chat structure:



model










share|improve this question


























    up vote
    0
    down vote

    favorite












    I check my database for a reference (which exists) and i don't get any chats back but I don't see where.



    chat.service.ts:



      getUserChats(): Observable<Chat> {
    let uid = '-1';
    this.auth.currUser.subscribe(user => (uid = user.uid));
    this.chats = this.afs
    .collection('chats', ref => ref.where('members', 'array-contains', `users/${uid}`))
    .snapshotChanges()
    .pipe(
    map(actions => {
    return actions.map(action => {
    const data = action.payload.doc.data() as Chat;
    const id = action.payload.doc.id;
    return {id, ...data};
    });
    })
    ) as Observable<Chat>;
    return this.chats;
    }


    The code is intented to get all the chats where the logged in user is a member in. I do this by checking the members array, if there is any reference to him.



    chat.component.ts:



    chats: Observable<Chat>;
    constructor(private router: Router, public chatService: ChatService, public dialog: MatDialog, public auth: AuthService) { }

    ngOnInit() {
    this.chats = this.chatService.getUserChats();
    }


    chat.component.html:



    <mat-list-option *ngFor="let chat of chats | async" (click)="handleClick(chatList)" [value]="chat.id">
    <img matListAvatar src="./assets/avatar.svg" alt="Avatar Icon">
    <h3 mat-line>{{chat.name}}</h3>
    <p mat-line>test</p>
    </mat-list-option>


    Chat structure:



    model










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I check my database for a reference (which exists) and i don't get any chats back but I don't see where.



      chat.service.ts:



        getUserChats(): Observable<Chat> {
      let uid = '-1';
      this.auth.currUser.subscribe(user => (uid = user.uid));
      this.chats = this.afs
      .collection('chats', ref => ref.where('members', 'array-contains', `users/${uid}`))
      .snapshotChanges()
      .pipe(
      map(actions => {
      return actions.map(action => {
      const data = action.payload.doc.data() as Chat;
      const id = action.payload.doc.id;
      return {id, ...data};
      });
      })
      ) as Observable<Chat>;
      return this.chats;
      }


      The code is intented to get all the chats where the logged in user is a member in. I do this by checking the members array, if there is any reference to him.



      chat.component.ts:



      chats: Observable<Chat>;
      constructor(private router: Router, public chatService: ChatService, public dialog: MatDialog, public auth: AuthService) { }

      ngOnInit() {
      this.chats = this.chatService.getUserChats();
      }


      chat.component.html:



      <mat-list-option *ngFor="let chat of chats | async" (click)="handleClick(chatList)" [value]="chat.id">
      <img matListAvatar src="./assets/avatar.svg" alt="Avatar Icon">
      <h3 mat-line>{{chat.name}}</h3>
      <p mat-line>test</p>
      </mat-list-option>


      Chat structure:



      model










      share|improve this question













      I check my database for a reference (which exists) and i don't get any chats back but I don't see where.



      chat.service.ts:



        getUserChats(): Observable<Chat> {
      let uid = '-1';
      this.auth.currUser.subscribe(user => (uid = user.uid));
      this.chats = this.afs
      .collection('chats', ref => ref.where('members', 'array-contains', `users/${uid}`))
      .snapshotChanges()
      .pipe(
      map(actions => {
      return actions.map(action => {
      const data = action.payload.doc.data() as Chat;
      const id = action.payload.doc.id;
      return {id, ...data};
      });
      })
      ) as Observable<Chat>;
      return this.chats;
      }


      The code is intented to get all the chats where the logged in user is a member in. I do this by checking the members array, if there is any reference to him.



      chat.component.ts:



      chats: Observable<Chat>;
      constructor(private router: Router, public chatService: ChatService, public dialog: MatDialog, public auth: AuthService) { }

      ngOnInit() {
      this.chats = this.chatService.getUserChats();
      }


      chat.component.html:



      <mat-list-option *ngFor="let chat of chats | async" (click)="handleClick(chatList)" [value]="chat.id">
      <img matListAvatar src="./assets/avatar.svg" alt="Avatar Icon">
      <h3 mat-line>{{chat.name}}</h3>
      <p mat-line>test</p>
      </mat-list-option>


      Chat structure:



      model







      javascript typescript firebase google-cloud-firestore






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 10:22









      filip

      140110




      140110
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote













          Apparently, you are querying for users/uid while it is /users/uid that is stored in your database, see the first /






          share|improve this answer





















          • Didn't work too, but good eyes!
            – filip
            Nov 7 at 13:14








          • 1




            FYI, I've just made some tests and slashes (/) in the array value do not create any problem (I suspected that maybe the / were the problem cause... but no). If I test your database model and values with a "pure" javascript code the query works well.
            – Renaud Tarnec
            Nov 7 at 13:23


















          up vote
          0
          down vote



          accepted










          The value '-1' will be searched for instantly because I'm not waiting for the User Observable to finish, that's why it searchs for -1 and finds nothing, so I have to wait for the user Observable. Still don't know how to return the inner observable then.






          share|improve this answer




























            up vote
            0
            down vote













            i think that you return "this.chat" from the service before the Observable comeback from the server .
            you need to return the Observable himself....
            try this change:



            getUserChats(): Observable < Chat > {
            let uid = '-1';
            this.auth.currUser.subscribe(user => (
            uid = user.uid;

            //that what you need to return
            return this.afs
            .collection('chats', ref => ref.where('members', 'array-
            contains ', `users/${uid}`))
            .snapshotChanges()
            .pipe(
            map(actions => {
            return actions.map(action => {
            const data = action.payload.doc.data() as Chat;
            const id = action.payload.doc.id;
            return { id,...data };
            });
            })
            ) as Observable < Chat > ;

            }

            ));


            each Observable need subscribe - but in angular the async pipe make it for you - so you just need to pass him Observable.



            i edit my answer - now the query waits to the answer from the user auth






            share|improve this answer























            • Tried it and didn't work
              – filip
              Nov 7 at 12:47










            • try with the new code
              – yehonatan yehezkel
              Nov 7 at 15:52










            • I don't want to subscribe, I want to get the Observable as a return value @yehonatan yehezkel
              – filip
              Nov 7 at 15:59











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53187513%2ffirestore-where-statement-with-array-containts-doesnt-work%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            Apparently, you are querying for users/uid while it is /users/uid that is stored in your database, see the first /






            share|improve this answer





















            • Didn't work too, but good eyes!
              – filip
              Nov 7 at 13:14








            • 1




              FYI, I've just made some tests and slashes (/) in the array value do not create any problem (I suspected that maybe the / were the problem cause... but no). If I test your database model and values with a "pure" javascript code the query works well.
              – Renaud Tarnec
              Nov 7 at 13:23















            up vote
            1
            down vote













            Apparently, you are querying for users/uid while it is /users/uid that is stored in your database, see the first /






            share|improve this answer





















            • Didn't work too, but good eyes!
              – filip
              Nov 7 at 13:14








            • 1




              FYI, I've just made some tests and slashes (/) in the array value do not create any problem (I suspected that maybe the / were the problem cause... but no). If I test your database model and values with a "pure" javascript code the query works well.
              – Renaud Tarnec
              Nov 7 at 13:23













            up vote
            1
            down vote










            up vote
            1
            down vote









            Apparently, you are querying for users/uid while it is /users/uid that is stored in your database, see the first /






            share|improve this answer












            Apparently, you are querying for users/uid while it is /users/uid that is stored in your database, see the first /







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 7 at 10:47









            Renaud Tarnec

            8,80121431




            8,80121431












            • Didn't work too, but good eyes!
              – filip
              Nov 7 at 13:14








            • 1




              FYI, I've just made some tests and slashes (/) in the array value do not create any problem (I suspected that maybe the / were the problem cause... but no). If I test your database model and values with a "pure" javascript code the query works well.
              – Renaud Tarnec
              Nov 7 at 13:23


















            • Didn't work too, but good eyes!
              – filip
              Nov 7 at 13:14








            • 1




              FYI, I've just made some tests and slashes (/) in the array value do not create any problem (I suspected that maybe the / were the problem cause... but no). If I test your database model and values with a "pure" javascript code the query works well.
              – Renaud Tarnec
              Nov 7 at 13:23
















            Didn't work too, but good eyes!
            – filip
            Nov 7 at 13:14






            Didn't work too, but good eyes!
            – filip
            Nov 7 at 13:14






            1




            1




            FYI, I've just made some tests and slashes (/) in the array value do not create any problem (I suspected that maybe the / were the problem cause... but no). If I test your database model and values with a "pure" javascript code the query works well.
            – Renaud Tarnec
            Nov 7 at 13:23




            FYI, I've just made some tests and slashes (/) in the array value do not create any problem (I suspected that maybe the / were the problem cause... but no). If I test your database model and values with a "pure" javascript code the query works well.
            – Renaud Tarnec
            Nov 7 at 13:23












            up vote
            0
            down vote



            accepted










            The value '-1' will be searched for instantly because I'm not waiting for the User Observable to finish, that's why it searchs for -1 and finds nothing, so I have to wait for the user Observable. Still don't know how to return the inner observable then.






            share|improve this answer

























              up vote
              0
              down vote



              accepted










              The value '-1' will be searched for instantly because I'm not waiting for the User Observable to finish, that's why it searchs for -1 and finds nothing, so I have to wait for the user Observable. Still don't know how to return the inner observable then.






              share|improve this answer























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                The value '-1' will be searched for instantly because I'm not waiting for the User Observable to finish, that's why it searchs for -1 and finds nothing, so I have to wait for the user Observable. Still don't know how to return the inner observable then.






                share|improve this answer












                The value '-1' will be searched for instantly because I'm not waiting for the User Observable to finish, that's why it searchs for -1 and finds nothing, so I have to wait for the user Observable. Still don't know how to return the inner observable then.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 7 at 13:10









                filip

                140110




                140110






















                    up vote
                    0
                    down vote













                    i think that you return "this.chat" from the service before the Observable comeback from the server .
                    you need to return the Observable himself....
                    try this change:



                    getUserChats(): Observable < Chat > {
                    let uid = '-1';
                    this.auth.currUser.subscribe(user => (
                    uid = user.uid;

                    //that what you need to return
                    return this.afs
                    .collection('chats', ref => ref.where('members', 'array-
                    contains ', `users/${uid}`))
                    .snapshotChanges()
                    .pipe(
                    map(actions => {
                    return actions.map(action => {
                    const data = action.payload.doc.data() as Chat;
                    const id = action.payload.doc.id;
                    return { id,...data };
                    });
                    })
                    ) as Observable < Chat > ;

                    }

                    ));


                    each Observable need subscribe - but in angular the async pipe make it for you - so you just need to pass him Observable.



                    i edit my answer - now the query waits to the answer from the user auth






                    share|improve this answer























                    • Tried it and didn't work
                      – filip
                      Nov 7 at 12:47










                    • try with the new code
                      – yehonatan yehezkel
                      Nov 7 at 15:52










                    • I don't want to subscribe, I want to get the Observable as a return value @yehonatan yehezkel
                      – filip
                      Nov 7 at 15:59















                    up vote
                    0
                    down vote













                    i think that you return "this.chat" from the service before the Observable comeback from the server .
                    you need to return the Observable himself....
                    try this change:



                    getUserChats(): Observable < Chat > {
                    let uid = '-1';
                    this.auth.currUser.subscribe(user => (
                    uid = user.uid;

                    //that what you need to return
                    return this.afs
                    .collection('chats', ref => ref.where('members', 'array-
                    contains ', `users/${uid}`))
                    .snapshotChanges()
                    .pipe(
                    map(actions => {
                    return actions.map(action => {
                    const data = action.payload.doc.data() as Chat;
                    const id = action.payload.doc.id;
                    return { id,...data };
                    });
                    })
                    ) as Observable < Chat > ;

                    }

                    ));


                    each Observable need subscribe - but in angular the async pipe make it for you - so you just need to pass him Observable.



                    i edit my answer - now the query waits to the answer from the user auth






                    share|improve this answer























                    • Tried it and didn't work
                      – filip
                      Nov 7 at 12:47










                    • try with the new code
                      – yehonatan yehezkel
                      Nov 7 at 15:52










                    • I don't want to subscribe, I want to get the Observable as a return value @yehonatan yehezkel
                      – filip
                      Nov 7 at 15:59













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    i think that you return "this.chat" from the service before the Observable comeback from the server .
                    you need to return the Observable himself....
                    try this change:



                    getUserChats(): Observable < Chat > {
                    let uid = '-1';
                    this.auth.currUser.subscribe(user => (
                    uid = user.uid;

                    //that what you need to return
                    return this.afs
                    .collection('chats', ref => ref.where('members', 'array-
                    contains ', `users/${uid}`))
                    .snapshotChanges()
                    .pipe(
                    map(actions => {
                    return actions.map(action => {
                    const data = action.payload.doc.data() as Chat;
                    const id = action.payload.doc.id;
                    return { id,...data };
                    });
                    })
                    ) as Observable < Chat > ;

                    }

                    ));


                    each Observable need subscribe - but in angular the async pipe make it for you - so you just need to pass him Observable.



                    i edit my answer - now the query waits to the answer from the user auth






                    share|improve this answer














                    i think that you return "this.chat" from the service before the Observable comeback from the server .
                    you need to return the Observable himself....
                    try this change:



                    getUserChats(): Observable < Chat > {
                    let uid = '-1';
                    this.auth.currUser.subscribe(user => (
                    uid = user.uid;

                    //that what you need to return
                    return this.afs
                    .collection('chats', ref => ref.where('members', 'array-
                    contains ', `users/${uid}`))
                    .snapshotChanges()
                    .pipe(
                    map(actions => {
                    return actions.map(action => {
                    const data = action.payload.doc.data() as Chat;
                    const id = action.payload.doc.id;
                    return { id,...data };
                    });
                    })
                    ) as Observable < Chat > ;

                    }

                    ));


                    each Observable need subscribe - but in angular the async pipe make it for you - so you just need to pass him Observable.



                    i edit my answer - now the query waits to the answer from the user auth







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 7 at 16:00

























                    answered Nov 7 at 10:50









                    yehonatan yehezkel

                    36339




                    36339












                    • Tried it and didn't work
                      – filip
                      Nov 7 at 12:47










                    • try with the new code
                      – yehonatan yehezkel
                      Nov 7 at 15:52










                    • I don't want to subscribe, I want to get the Observable as a return value @yehonatan yehezkel
                      – filip
                      Nov 7 at 15:59


















                    • Tried it and didn't work
                      – filip
                      Nov 7 at 12:47










                    • try with the new code
                      – yehonatan yehezkel
                      Nov 7 at 15:52










                    • I don't want to subscribe, I want to get the Observable as a return value @yehonatan yehezkel
                      – filip
                      Nov 7 at 15:59
















                    Tried it and didn't work
                    – filip
                    Nov 7 at 12:47




                    Tried it and didn't work
                    – filip
                    Nov 7 at 12:47












                    try with the new code
                    – yehonatan yehezkel
                    Nov 7 at 15:52




                    try with the new code
                    – yehonatan yehezkel
                    Nov 7 at 15:52












                    I don't want to subscribe, I want to get the Observable as a return value @yehonatan yehezkel
                    – filip
                    Nov 7 at 15:59




                    I don't want to subscribe, I want to get the Observable as a return value @yehonatan yehezkel
                    – filip
                    Nov 7 at 15:59


















                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53187513%2ffirestore-where-statement-with-array-containts-doesnt-work%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()