Javascript 如何在react native上从firestore检索数据
我试图从firestore上的集合中检索数据,但当我将此数据放入诸如Text之类的组件时,我会收到一个错误,然而,当我将相同的数据放入console.log()时,我可以毫无问题地使用其值 我的代码Javascript 如何在react native上从firestore检索数据,javascript,firebase,react-native,Javascript,Firebase,React Native,我试图从firestore上的集合中检索数据,但当我将此数据放入诸如Text之类的组件时,我会收到一个错误,然而,当我将相同的数据放入console.log()时,我可以毫无问题地使用其值 我的代码 const nome = firestore().collections('usuários').doc(auth().currentUser.uid).get().then( (documentSnapshot) => documentSnapshot.get('nome')) retur
const nome = firestore().collections('usuários').doc(auth().currentUser.uid).get().then(
(documentSnapshot) => documentSnapshot.get('nome'))
return (
<View style={{ flex: 1 }}>
<View style={styles.imageContainer}>
<Image source={require('../assets/img/user-bg.jpg')} style={styles.image} />
</View>
<View style={{ flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }}>
<View style={styles.userLabel}>
<Text style={[styles.user, { color: colors.text }]}>{nome}</Text>
<Text style={[styles.email, { color: colors.text }]}>email</Text>
</View>
<CaretDownIcon width={8} height={8} fill={colors.text} style={{ marginRight: 10 }}/>
</View>
</View>
</View>
)
const nome=firestore().collections('usuários').doc(auth().currentUser.uid).get()。然后(
(documentSnapshot)=>documentSnapshot.get('nome'))
返回(
{nome}
电子邮件
)
要获得nome,您必须:
const nome = firestore().collections('usuários').doc(auth().currentUser.uid).get().then(
(documentSnapshot) => documentSnapshot.get('nome'))
这不会返回名称!相反:
firestore().collections('usuários').doc(auth().currentUser.uid).get()
返回一个Promise
。因此,您希望发生的是:
.get()
获取名称.get()
和。然后()
返回一个承诺
,这是一个对象
(因此会出现该错误)对象呈现
name
中的函数,即。
(documentSnapshot)=>documentSnapshot.get('nome')
{nome}
进入{this.state.nome}
(documentSnapshot)=>documentSnapshot.get('nome')
更改为(documentSnapshot)=>{let _nome=documentSnapshot.get('nome');this.setState({nome:_nome})
setState
setState
时自动用新名称更新页面名称,可以尝试类似{this.state.nome | | |“等待响应…”
如果您现在正在使用ES6函数呈现程序,则需要使用React类