简体   繁体   English

如何在本机反应中从firestore检索数据

[英]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:所以你想要发生的是:

  1. Fetch the name取名字
  2. Display the name显示名称

But what is happening instead is:但实际上发生的是:

  1. You ask firebase to fetch the name with .get() .您要求 firebase 使用.get()获取名称。 .get() and .then() return a Promise , which is an Object (hence why you're getting that error) .get()和 .then .then()返回一个Promise ,这是一个Object (因此你为什么会得到那个错误)
  2. Your page renders with the Object您的页面使用Object呈现
  3. Firebase finishes fetching the name and then calls the function in name , ie (documentSnapshot) => documentSnapshot.get('nome') Firebase 完成获取名称,然后调用name中的 function,即(documentSnapshot) => documentSnapshot.get('nome')

So instead, consider:因此,请考虑:

  1. Change <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>
  2. Change (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然后发生的事情是

  1. You ask firebase to fetch the name你问firebase取名字
  2. Your page renders, but firebase doesn't have your name yet.您的页面呈现,但 firebase 还没有您的名字。 (it'll show up as undefined, but that's ok, because then: (它会显示为未定义,但没关系,因为那时:
  3. Firebase fetches your name and then calls setState Firebase 获取您的姓名,然后调用setState
  4. React updates the page for you automatically with the new name when setState is called当调用setState时,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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM