【问题标题】:Firebase Insert is happening instead of update, no error is shownFirebase 插入发生而不是更新,没有显示错误
【发布时间】:2020-04-30 11:01:57
【问题描述】:

我正在尝试使用 react native 更新 firebase 数据库中的字段,而是在单击按钮时插入新条目。

import React,{Component} from 'react';
import {  Platform, StyleSheet, StatusBar, View, Text, TextInput, Button, Alert } from 'react-native';
import {editAp} from '../pages/func';
import firebase from 'firebase';
if (!firebase.apps.length) {
 firebase.initializeApp({});
}
class Edit1 extends Component {
constructor(props){
super(props);
this.state = {
    email:this.props.navigation.state.params.email,
    password:this.props.navigation.state.params.password,
    keyis:this.props.navigation.state.params.keyis,
 };
}
submit1=()=>{
    console.log(this.props.navigation.state.params.keyis)
    firebase.database().ref('/users1').child(this.props.navigation.state.params.keyis).update(({email:this.state.email,password:this.state.password}))
    Alert.alert('Action!', 'user updated');
}
render(){
  return (
        <View style={styles.container}>
            <Text>Edit Here</Text>
            <TextInput style={{marginTop:20, height:40, borderColor:'gray', borderWidth:1}} onChangeText={email=>this.setState({email})} value={this.state.email}/>
            <TextInput style={{marginTop:20, height:40, borderColor:'gray', borderWidth:1}} onChangeText={password=>this.setState({password})} value={this.state.password}/>
            <TextInput style={{marginTop:20, height:40, borderColor:'gray', borderWidth:1}} onChangeText={keyis=>this.setState({keyis})} value={this.state.keyis}/>
       <Button title='Submit' onPress={this.submit1} />
        </View>
    );
    }
}
 const styles = StyleSheet.create({
 container: {
 flex:1,
 justifyContent: 'center',
 backgroundColor: '#fff',
 padding: 30,
 },
 });
 export default Edit1;

我不知道我在 textinput 字段和控制台上获得的更新功能电子邮件、密码、keyis 值有什么问题,submit1 功能有什么问题? 数据库结构如下:

插入代码:

    import React,{Component} from 'react';
import {  Platform, StyleSheet, StatusBar, View, Text, TextInput, Alert, Button, TouchableOpacity, FlatList } from 'react-native';
import firebase from 'firebase';
if (!firebase.apps.length) {    firebase.initializeApp({}); }
class Form2 extends Component<{}> {
  constructor(props){
    super(props);
   this.state = {
     email:'',
    password:'',
      };  }
   addNew = () => {
    firebase.database().ref('/users1').push({
      email: this.state.email,
      password: this.state.password,

    });
    Alert.alert('Action!', 'user added');
    this.setState({
      email:'',
      password:'',
});  }
   render() {
  return(
<View style={styles.container}>
<Text style={styles.inpt}>Insert Ravel</Text>
<TextInput style={styles.inputBox1} underlinerColorAndroid='rgba(0,0,0,0)'
placeholder="Email"  onChangeText={e => { this.setState({  email: e,            });          }}     />
<TextInput style={styles.inputBox2} underlinerColorAndroid='rgba(0,0,0,0)'  placeholder="password" 
onChangeText={e => { this.setState({ password: e,            });       }}   />
<View style={styles.button1}>
<Button title='Insert Record' onPress={this.addNew.bind(this)}/>
</View></View>  )   } }
export default Form2;

【问题讨论】:

标签: javascript reactjs firebase react-native firebase-realtime-database


【解决方案1】:

我认为您需要在更新代码中添加一些 id:

firebase.database().ref('/users1/'+userId)

所以可能是 firebase 插入新数据,因为它不知道要更新的行的 id

【讨论】:

  • 我从哪里获取这个 userId,因为已经唯一的 id 是 keyis 并且写在 child 中,并且对于他们特定的 keyis(db 中每一行的唯一 id)我想更新电子邮件和密码字段.使用以下代码: firebase.database().ref('/users1'+this.props.navigation.state.params.keyis).update(({email:this.state.email,password:this.state.password} )) 新值被插入
  • @ChintanBhuta 我忘了添加另一个斜线。请尝试新代码
  • firebase.database().ref('/users1'/+this.props.navigation.state.params.keyis).update(({email:this.state.email,password:this .state.password})) 所以我应该这样做吗?
  • 使用上面的代码,它将新值插入数据库而不是更新现有的@kafinsalim
【解决方案2】:

尝试用我的代码替换您的代码:

注册部分:

import React,{Component} from 'react';
import {  Platform, StyleSheet, StatusBar, View, Text, TextInput, Alert, Button, TouchableOpacity, FlatList } from 'react-native';
import firebase from 'firebase';
if (!firebase.apps.length) {    firebase.initializeApp({}); }
class Form2 extends Component<{}> {
  constructor(props){
    super(props);
   this.state = {
     email:'',
    password:'',
      };  }
   addNew = () => {

    //MODIFIED PATH HERE
    firebase.database().ref('/users1/'+firebase.auth().currentUser.uid).set({

      email: this.state.email,
      password: this.state.password,

    });
    Alert.alert('Action!', 'user added');
    this.setState({
      email:'',
      password:'',
});  }

//This will creating node with user UID as value

更新活动代码:

import React,{Component} from 'react';
import {  Platform, StyleSheet, StatusBar, View, Text, TextInput, Button, Alert } from 'react-native';
import {editAp} from '../pages/func';
import firebase from 'firebase';
if (!firebase.apps.length) {
 firebase.initializeApp({});
}
class Edit1 extends Component {
constructor(props){
super(props);
this.state = {
    email:this.props.navigation.state.params.email,
    password:this.props.navigation.state.params.password,
    keyis:this.props.navigation.state.params.keyis,
 };
}
submit1=()=>{
    console.log(this.props.navigation.state.params.keyis)

//Modified path here also

firebase.database().ref('/users1/').child(firebase.auth().currentUser.uid).update(({email:this.state.email,password:this.state.password}))
        Alert.alert('Action!', 'user updated');
    }
    render(){
      return (
            <View style={styles.container}>
                <Text>Edit Here</Text>
                <TextInput style={{marginTop:20, height:40, borderColor:'gray', borderWidth:1}} onChangeText={email=>this.setState({email})} value={this.state.email}/>
                <TextInput style={{marginTop:20, height:40, borderColor:'gray', borderWidth:1}} onChangeText={password=>this.setState({password})} value={this.state.password}/>
                <TextInput style={{marginTop:20, height:40, borderColor:'gray', borderWidth:1}} onChangeText={keyis=>this.setState({keyis})} value={this.state.keyis}/>
           <Button title='Submit' onPress={this.submit1} />
            </View>
        );
        }
    }
     const styles = StyleSheet.create({
     container: {
     flex:1,
     justifyContent: 'center',
     backgroundColor: '#fff',
     padding: 30,
     },
     });
     export default Edit1;

我所做的只是修改了您的数据路径。我假设您的用户已注册,因为我在您的注册码中看不到任何注册内容。

只需更改路径。其余代码相同。这将使用用户 UID 而不是数据库 ID 创建节点。 让我知道它是否有效。不要忘记创建新用户。

【讨论】:

  • currentUser 中的错误。注册代码中该行中的意外令牌,预期为“,”
  • @ChintanBhuta 请在/users1/之后添加+。我也编辑了我的代码。
  • @ChintanBhuta 那行现在应该变成:ref('/users1/'+firebase.auth().currentUser.uid)
  • 您的代码中有错误,我之前也遇到过,此代码根本不起作用 typeError: null 不是对象(正在评估'_firebase.drfault.auth().currentUser.uid')。您需要更改 firebase 语法以使用键或子作为其方法的更新查询,这种方式不起作用
  • @ChintanBhuta drfault 这个词是从哪里来的??只需在此处分享 Google Drive 链接
猜你喜欢
  • 2016-12-13
  • 1970-01-01
  • 1970-01-01
  • 2015-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多