i am trying to fetch chatList from firebase and for each chat, get username from users doc, but the array stored in the chats
state is not iterating…
and if i remove await from the query which fetches name
of freind then this chats
is iterable but in this case, value of temp
or freind
property of chats
/ chatList
is undefined
const Chat = () => {
const { currentUser } = useAuth();
const [chats, setChats] = useState([])
const [loading, setLoading] = useState(true)
useEffect(() => {
const chatListQuery = firestore()
.collection('chatRoom')
.where('members', 'array-contains', currentUser.uid)
.where('group', '==', false)
.orderBy('recentMessage.sendAt', 'desc');
const unsubscribe =
chatListQuery.onSnapshot((querySnapShot) => {
const chatList = []
querySnapShot.forEach(async (item) => {
const temp = [];
const freindId = item.data().members.filter((member) => member !== currentUser.uid);
await firestore().collection('users').doc(freindId[0]).get().then((snapshot) => {
temp.push(
snapshot.data().name,
);
// setFreind(snapshot.data().name)
});
chatList.push({ ...item.data(), id: item.id, freind: temp });
});
setLoading(false)
setChats(chatList)
});
return () => unsubscribe()
}, [])
return (
<View>
{loading ? (
<Text>Loading...</Text>
) : (
<>
{console.log(typeof chats)}
{/*
above log displays this but either `chats.map` or `FlatList` is not iterating over this
[]
0: {timestamp: FirestoreTimestamp, group: false, members: Array(2), recentMessage: {…}, id: '5h4N0KcE6KYLiDA4esNv', …}
1: {group: false, timestamp: FirestoreTimestamp, members: Array(2), recentMessage: {…}, id: 'RB9EIedrr4vp3JyGSmUa', …}
2: {timestamp: FirestoreTimestamp, group: false, members: Array(2), recentMessage: {…}, id: 'LapcO2Zc0Qv4Kyx7jeiu', …}
3: {group: false, timestamp: FirestoreTimestamp, members: Array(2), recentMessage: {…}, id: 'h1i8lTbPmnbZTgG2mwDV', …}
length: 4
[[Prototype]]: Array(0)
*/}
<FlatList data={chats} renderItem={({ item }) => <Text>{item.freind}</Text>} keyExtractor={item => item.id} />
</>
)}
</View>
)
}
export default Chat;
below is the log value of chats
when using await
[]
0: {timestamp: FirestoreTimestamp, group: false, members: Array(2), recentMessage: {…}, id: '5h4N0KcE6KYLiDA4esNv', …}
1: {group: false, timestamp: FirestoreTimestamp, members: Array(2), recentMessage: {…}, id: 'RB9EIedrr4vp3JyGSmUa', …}
2: {timestamp: FirestoreTimestamp, group: false, members: Array(2), recentMessage: {…}, id: 'LapcO2Zc0Qv4Kyx7jeiu', …}
3: {group: false, timestamp: FirestoreTimestamp, members: Array(2), recentMessage: {…}, id: 'h1i8lTbPmnbZTgG2mwDV', …}
length: 4
[[Prototype]]: Array(0)
below is the log value of chats
when i do not use async/await
(4) [{…}, {…}, {…}, {…}]
0: {timestamp: FirestoreTimestamp, group: false, members: Array(2), recentMessage: {…}, id: '5h4N0KcE6KYLiDA4esNv', …}
1: {group: false, timestamp: FirestoreTimestamp, members: Array(2), recentMessage: {…}, id: 'RB9EIedrr4vp3JyGSmUa', …}
2: {timestamp: FirestoreTimestamp, group: false, members: Array(2), recentMessage: {…}, id: 'LapcO2Zc0Qv4Kyx7jeiu', …}
3: {group: false, timestamp: FirestoreTimestamp, members: Array(2), recentMessage: {…}, id: 'h1i8lTbPmnbZTgG2mwDV', …}
length: 4
[[Prototype]]: Array(0)
but in this case, value of item.freind[0]
is undefined
2
Answers
Alternate Solution:
Try to use this useEffect code