[英]How to retrieve data from firestore on react native
I'm trying to retrieve data from my collection on firestore but when i put this data inside a component such as Text i get an error, however when ai put this same data on console.log() i can use its value with no problems.我正在尝试从我在 firestore 上的集合中检索数据,但是当我将此数据放入诸如 Text 之类的组件中时出现错误,但是当我将相同的数据放在 console.log() 上时,我可以毫无问题地使用它的值.
My code我的代码
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>
)
In order to get nome, you have:为了获得名字,你有:
const nome = firestore().collections('usuários').doc(auth().currentUser.uid).get().then(
(documentSnapshot) => documentSnapshot.get('nome'))
This doesn't return a name: Instead: firestore().collections('usuários').doc(auth().currentUser.uid).get()
returns a Promise
.这不会返回名称:而是:
firestore().collections('usuários').doc(auth().currentUser.uid).get()
返回Promise
。 So what you want to happen is:所以你想要发生的是:
But what is happening instead is:但实际上发生的是:
.get()
..get()
获取名称。 .get()
and .then()
return a Promise
, which is an Object
(hence why you're getting that error) .get()
和 .then .then()
返回一个Promise
,这是一个Object
(因此你为什么会得到那个错误)Object
Object
呈现name
, ie (documentSnapshot) => documentSnapshot.get('nome')
name
中的 function,即(documentSnapshot) => documentSnapshot.get('nome')
So instead, consider:因此,请考虑:
<Text style={[styles.user, { color: colors.text }]}>{nome}</Text>
into <Text style={[styles.user, { color: colors.text }]}>{this.state.nome}</Text>
<Text style={[styles.user, { color: colors.text }]}>{nome}</Text>
改为<Text style={[styles.user, { color: colors.text }]}>{this.state.nome}</Text>
(documentSnapshot) => documentSnapshot.get('nome')
to (documentSnapshot) => {let _nome = documentSnapshot.get('nome'); this.setState({nome:_nome})
(documentSnapshot) => documentSnapshot.get('nome')
更改为(documentSnapshot) => {let _nome = documentSnapshot.get('nome'); this.setState({nome:_nome})
(documentSnapshot) => {let _nome = documentSnapshot.get('nome'); this.setState({nome:_nome})
And then what happens is that然后发生的事情是
setState
setState
setState
is calledsetState
时,React 会使用新名称自动为您更新页面If you don't want an undefined
name, you can try something like <Text>{this.state.nome || "Waiting for response..."}</Text>
如果您不想要
undefined
的名称,可以尝试类似<Text>{this.state.nome || "Waiting for response..."}</Text>
<Text>{this.state.nome || "Waiting for response..."}</Text>
. <Text>{this.state.nome || "Waiting for response..."}</Text>
。
If you're using an ES6 function renderer right now, you'll need to use a React Class instead.如果您现在使用的是 ES6 function 渲染器,则需要改用 React Class。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.