I use componentDidMount to start fetchNewUser when I navigate to the screen, it is supposed to recognize a new user and update it if it true, the problem is that when I navigate in the screen for the first time it returns undefined is not an object (evaluating 'snapshot.data().newUser'), but if I refresh the screen it works.
fetchNewUser = async () => {
const fetchInformation = doc(db, 'path')
await getDoc(fetchInformation)
.then( async (snapshot) => {
const newUser = await snapshot.data().newUser
const address = await snapshot.data().mainAddress
console.log(newUser)
if (newUser == true || newUser == undefined) {
this.props.navigation.navigate('AddressSearch')
updateDoc(fetchInformation, {
newUser: false
})
} else {
this.setState({address: address})
}
})
.catch((err) => console.log(err))
}
The problem is that you are trying to acces newUser on the promise, what you should do instead is const newUser = (await snapshot.data()).newUser