undefined在导航到屏幕时不是对象(评估"快照.data().newUser")



当我导航到屏幕时,我使用componentDidMount来启动fetchNewUser,它应该识别新用户并更新它,如果它是真的,问题是当我第一次在屏幕中导航时,它会返回undefined is not an object (evaluating 'snapshot.data().newUser'),但如果我刷新屏幕,它会工作。

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

问题是您正试图在promise上访问newUser,而您应该做的是const newUser = (await snapshot.data()).newUser

相关内容

最新更新