'undefined is not an object (evaluating 'snapshot.data().newUser') when navigating to Screen

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))
      }


Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pasca Marius