07-31-2023, 09:44 AM
I am trying to migrate from real-time database to firestore in react-native with expo. Why won't the foreach loop work now?
**RTDB**
firebase.database().ref('messages').child(this.chatID).on('value', snap => {
let messages = []
snap.forEach(message => {
messages.push(message.val())
})
this.setState({messages: messages})
})
**Firestore**
const unsubChat = firebase.firestore().collection('messages').doc(this.chatID).onSnapshot(docSnapshot => {
let messages = []
const myMesgs = docSnapshot.data()
if (docSnapshot.data()){
myMesgs.forEach(message => {
console.log(message)
})
}
})
snap and myMesgs are identical objects. I get a function undefined error when trying to use .foreach on myMesgs. I want to push the value of each object inside myMesgs into an array.
[![enter image description here][1]][1]
[1]:
**RTDB**
firebase.database().ref('messages').child(this.chatID).on('value', snap => {
let messages = []
snap.forEach(message => {
messages.push(message.val())
})
this.setState({messages: messages})
})
**Firestore**
const unsubChat = firebase.firestore().collection('messages').doc(this.chatID).onSnapshot(docSnapshot => {
let messages = []
const myMesgs = docSnapshot.data()
if (docSnapshot.data()){
myMesgs.forEach(message => {
console.log(message)
})
}
})
snap and myMesgs are identical objects. I get a function undefined error when trying to use .foreach on myMesgs. I want to push the value of each object inside myMesgs into an array.
[![enter image description here][1]][1]
[1]: