I’m trying to add a feature to my chat app to allow the user to go back to chat rooms they recently visited, and created a message in. Right now I’m just trying to display names the chat rooms on the page.
my firebase date base is structured like this.
-ChatRooms(collection)
-"chat room name" (document)
-room(name of chat room)
-uid
-messages(subcollection)
-created
-room(name of chat room)
-text
-uid
-user(display name)
-userPic
I’m trying to get inside the "messages" subcollection, so I can query it to only display the names of the chat Rooms the user has visited. If the "uid" of the document inside the messages subCollection matches the "uid" of firebase "auth" currentUser.uid.
this is my code.
const [chatRoomData, setChatRoomData] = useState([]);
const [firebaseUserInfo, setFirebaseUserInfo] = useState(auth.currentUser);
useEffect(() =>{
onAuthStateChanged(auth, currentUser => {
setFirebaseUserInfo(currentUser);
const chatRoomCollectionRef = collection(dataBase, "ChatRooms/room/messages");
console.log(chatRoomCollectionRef)
const queryChatRooms = query(chatRoomCollectionRef, where("uid", "==", currentUser.uid));
const unSubscribe = onSnapshot(queryChatRooms, (snapshot) => {
const chatRooms = [];
snapshot.forEach((doc) => {
chatRooms.push({...doc.data(), id: doc.id});
});
setChatRoomData(chatRooms);
})
return () => unSubscribe();
})
}, []);
return (
<>
<div className="chats-container">
<header className='header-container'>
<h1 className="header">Your Chats</h1>
</header>
{chatRoomData.map((data) =>(
<div className='users-chatRooms-container' key={data.id}>
<button className='users-chatRooms'>{data.chatRoom}</button>
</div>
))}
</div>
</>
);
}
I tried this and nothing displays on the page. What is wrong with my code?
2
Answers
you need to reference each "room" document and then access its "messages" subcollection. Also, make sure you’re using the correct field names. Try the following code:
Make sure to adjust the import statements according the firbease. Ask if you’ve any doubts.
Here’s the best example I can do without seeing the data
Make sure to replace ‘your-firebase-config’ with your actual Firebase configuration. If this doesn’t solve the issue, consider checking your Firestore structure and adjust the code accordingly.
OR if the issue is just printing the nexted obj
You can solve this with recursion, here’. an example: