【问题标题】:Data is not store on Cloud Firestore using Android数据未使用 Android 存储在 Cloud Firestore 上
【发布时间】:2021-09-07 03:23:36
【问题描述】:

我想知道为什么在我的 Android 设备上测试 Expo Go 上的应用程序时,我的数据没有存储在 Firebase 数据库中。 在 Web 浏览器上测试时的 data is still being stored normally

这是代码

import React, { Component } from 'react'
import { View, Button, TextInput, StyleSheet, TouchableOpacity, Text  } from 'react-native'
import firebase from 'firebase'
import "firebase/firestore";

export class Register extends Component {

constructor(props) {
    super(props);

    this.state = {
        email: '',
        password: '',
        firstname: '',
        lastname:'',
        city:'',
        province:'',
    }

    this.onSignUp = this.onSignUp.bind(this)
}

onSignup() 函数

onSignUp() {
    const { email, password, firstname, lastname, city, province } = this.state;
    firebase.auth().createUserWithEmailAndPassword(email, password)
        .then((result) => {
            const user = firebase.auth().currentUser;
            firebase.firestore().collection("users")
                .doc(user.uid)
                .set({firstname,lastname,city,province,email})
            user.sendEmailVerification
            console.log(result)
        })
        .catch((error) => {
            console.log(error)
        })
}

数据输入(只显示电子邮件,因为它们都是一样的)

<View style={styles.textBoxView}>
     <TextInput
          style={styles.textBox}
          placeholder="E-mail"
          onChangeText={(email) => this.setState({ email })}/>
     <View style={styles.space}></View>
</View>

哦,还有注册按钮

<View style={styles.buttons}>
     <TouchableOpacity onPress={() => this.onSignUp()}>
         <Text style={styles.textElement}>Sign up</Text>
     </TouchableOpacity>
</View>

【问题讨论】:

  • 嘿,我已经发布了答案。如果您认为这对您有帮助,您可以通过单击对勾图标接受它,以便其他人知道问题已解决,否则请随时提出更多问题。

标签: javascript react-native google-cloud-firestore firebase-authentication


【解决方案1】:

您可以尝试处理第二个承诺,它使用.then() 向承诺添加数据吗?

onSignUp() {
    const { email, password, firstname, lastname, city, province } = this.state;
    firebase.auth().createUserWithEmailAndPassword(email, password)
        .then((result) => {
            const user = firebase.auth().currentUser;
            firebase.firestore().collection("users")
                .doc(user.uid)
                .set({firstname,lastname,city,province,email})
                .then(() => {
                    //user.sendEmailVerification
                })
                .catch(e => console.log(e))
        })
        .catch((error) => {
            console.log(error)
        })
}

user.sendEmailVerification 看起来也不完整。这也返回了一个承诺,理想情况下应该处理:

user.sendEmailVerification().then(() => {
  console.log("Verification Email Sent")
}).catch((e) => {
  console.log(e)
})

【讨论】:

  • 嗨,firebase.firestore().collection("users").doc(user.uid) .set({firstname,lastname,city,province,email}) 的部分是将数据添加到 Firestore 以及创建 users 集合的位置。那么我必须在then 块中添加什么?
猜你喜欢
  • 1970-01-01
  • 2021-08-27
  • 2019-01-08
  • 2018-07-06
  • 2020-10-12
  • 2020-03-01
  • 2021-03-29
  • 2021-09-03
  • 2021-05-01
相关资源
最近更新 更多