0

I want to do a WHERE foo = bar AND something = also_something in firebase

export const getAllRoomsWhichHaveUnreadMessages = (currentUserId) => {

    return query(
        roomsRef,
        where(USERS_PATH, 'array-contains', currentUserId), 
        where('unreadCount', '>', 0)
    )
}

How do I chain WHEREs and also how do I retrieve them? My above code works but it only outputs a query, how do I run the query ?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You may have a look at this stackoverflow [link](https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase) – Sathi Aiswarya Jul 18 '22 at 12:27

1 Answers1

1

You will need to use the getDocs method to get a snapshot of the query.

You can then map the docs of that snapshot.

import { query, where, getDocs } from "firebase/firestore";
    export const getAllRoomsWhichHaveUnreadMessages = async (currentUserId) => {

    const q = query(
        roomsRef,
        where(USERS_PATH, "array-contains", currentUserId), 
        where('unreadCount', '>', 0)
    )

    const querySnap = await getDocs(q);
    return querySnap.docs.map((doc) => ({
          id: doc.id,
          ...doc.data(),
        }));
    }
Min
  • 528
  • 1
  • 7
  • 26