As the title says. I'm using firestore for my chat service. I'm attempting to set up a way to filter out messages when the firestore updates
const users = useSelector((state) => state.assignUser);
const [messages, setMessages] = useState([]);
const chatID = 'KfM3GNxeVATi6sNFFUEG'
function getIDs(messageObj){
return messageObj.map(message => message._id)
}
useEffect(() => {
const messagesArray = []
db.collection('messages').doc(chatID).collection('messages').get().then(snapshot => {
snapshot.forEach(doc => messagesArray.push(doc.data()))
setMessages(messagesArray)
async function fetchMessages(){
const messageIDs = await getIDs(messages)
db.collection('messages').doc(chatID).collection('messages').onSnapshot(doc => {
doc.forEach((document) => {
if (messageIDs.indexOf(document.data()._id !== -1)) console.log('the is is ', messageIDs.indexOf(document.data()._id))
else console.log('notfoundssw')
})
})
}
fetchMessages()
})
}, [])
The await doesn't await the getIDs function and the log of messageIDs.indexOf(document.data()._id)
is -1 for every entry. I can't figure out why the async/await missbehaves