I have a Firestore listener to grab chat messages for which I use a limit of 10. Everything works well on first load. However, when there are 10 messages and the limit is reached, the chat does not accept new messages. It is as if the limit function accepts the first 10 messages and then will not take any more when I need it to be updated with every new message. Here's the listener code:
startChat () { document.getElementById("myForm").style.display = "block"; const ref = firebase.firestore().collection('Chats').doc(this.state.uid).collection('Messages'); const query = ref.orderBy('timestamp', 'asc').limit(10) this.unsubFromMessages = query.onSnapshot((snapshot) => { console.log(snapshot.docs.map(doc => {return doc.data()})) if (snapshot.empty) { console.log('No matching documents.'); firebase.firestore().collection('Chats').doc(this.state.uid). set({ name: this.state.displayName, uid: this.state.uid, email: this.state.email }).then(console.log("info saved")) .catch((error) => { console.log("Error saving info to document: ", error); }); } snapshot.docChanges().forEach((change) => { if (change.type === 'removed') { console.log(change.doc.data().content) } else if (change.type === 'added') { this.setState(state => { const messages = [...state.messages, {id: change.doc.id, body: change.doc.data()}] return { messages } }) setTimeout( this.scrollToBottom(), 2000) } }); }, (error) => {console.log(error)}); }
Does anyone know why this is happening and how to make the limit function accept new messages? Thanks.
https://stackoverflow.com/questions/66927623/why-does-firestores-limit-not-update April 03, 2021 at 12:08PM
没有评论:
发表评论