【问题标题】:save textipnput data to firebase using react-native for mobile app使用 react-native for mobile app 将 textipnput 数据保存到 firebase
【发布时间】:2019-08-07 05:22:43
【问题描述】:

我已经尝试将表单值保存到 firebase 有一段时间了,但它没有通过。如果你能帮忙,请帮忙。这就是我到目前为止所做的。

// SignUp.js
import firebase from '@firebase/app';
import '@firebase/auth';
import "@firebase/database";
//import {app,db} from '../config';
import { db } from '../config';
import React, { Component } from 'react';  
import {Alert, StyleSheet, Text, TextInput, View, Button } from 'react-native';

let addItem = (Name,Password,Email) => {  
      db.ref('/items').push({
        name: Name,
        password:Password,
        email:Email
      });
    };

export default class SignUp extends Component {
  state = { 
            email: '', 
            password: '', 
            name:'',
            errorMessage: null 
          };


  handleSubmit = () => {
    addItem(this.state.name,
            this.state.password,
            this.state.email
            );
    alert('Item saved successfully');
  };


render() {
    return (
      <View style={styles.container}>
        <Text>Sign Up</Text>
        {this.state.errorMessage &&
          <Text style={{ color: 'red' }}>
            {this.state.errorMessage}
          </Text>}
        <TextInput
          placeholder="Email"
          autoCapitalize="none"
          style={styles.textInput}
          onChangeText={(email) => this.setState({ email })}
          value={this.state.email}
        />
        <TextInput
          placeholder="Name"
          autoCapitalize="none"
          style={styles.textInput}
          onChangeText={(name) => this.setState({ name })}
          value={this.state.name}
        />
        <TextInput
          secureTextEntry
          placeholder="Password"
          autoCapitalize="none"
          style={styles.textInput}
          onChangeText={(password) => this.setState({ password })}
          value={this.state.password}
        />
        <Button title="Sign Up" onPress={this.handleSubmit} />
        <Button
          title="Already have an account? Login"
          onPress={() => this.props.navigation.navigate('Login')}
        />
      </View>
    )
  }
}

我的 db 配置文件正在从另一个配置文件中调用。 我已经确认了配置文件和数据库的链接,它正在工作。我在另一个 addFile.js 上使用过它,它运行良好。所以请大家,如果有人可以帮助我,我将不胜感激。如果有任何关于使用 react-native 和 firebase 的材料的建议,我将不胜感激。如果 firebase 对我来说不是最好的(因为我正在开发一个投资应用程序,我希望获得其他数据库的建议,例如 mongodb 以及如何开始的参考资料。)

【问题讨论】:

    标签: firebase react-native firebase-realtime-database firebase-authentication


    【解决方案1】:

    您好,您处理注册的最简单方法是使用 firebase 身份验证,这允许您使用 firebase 创建用户并安全地存储所有这些数据。您以后可以随时访问它并将其保存在不同的位置。 (确保在 firebase 身份验证中启用了电子邮件身份验证)

      handleSubmit = () => {
        firebase
      .auth()
      .createUserWithEmailAndPassword(this.state.email, this.state.password)
      .then((user) => {
        if(user.user) {
          user.user.updateProfile({
            displayName: this.state.name
          }).then(() => {
              alert('Item saved successfully');
            })
        }
      })
      .catch(error => this.setState({ errorMessage: error.message }));
    };
    

    完成此操作后,您可以稍后访问用户数据,如下所示

    const user = firebase.auth().currentUser;
    var postsRef = ref.child("/items/");
    var newPostRef = postsRef.push();
    newPostRef.set({
        name: user.displayName,
        email: user.email
    });
    

    用户现在将保存您需要的所有数据。请注意,您将无法访问密码,因为它是经过哈希处理的,并且永远不应像这样将其保存在数据库中。这就是为什么 firebase 限制对它的访问。我已经为您使用了其中一个示例,您可以在此处找到更多关于推送的信息。 https://firebase.google.com/docs/database/admin/save-data

    【讨论】:

    • 我想知道如何将其他用户条目存储到 firebase,而不仅仅是使用电子邮件和密码进行注册。假设我希望我的客户使用他们的客户代码和电话号码进行注册,我该怎么做。或者可以说我不想包含在登录期间将发送到他们手机的 otp,我该怎么做。所以这个想法是从firebase发送和检索值。非常感谢您回答的第二部分,它为我解决了一个大问题。
    • 嗨,如果这是一个有效的答案,请为未来的用户添加 V
    猜你喜欢
    • 2022-11-25
    • 2017-10-30
    • 2018-02-27
    • 1970-01-01
    • 2019-06-09
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多