【问题标题】:Unable to list data sent with push无法列出通过推送发送的数据
【发布时间】:2021-09-09 15:08:46
【问题描述】:

[]

我有 3 个录制屏幕,我想在一个页面上列出用户信息。但每个屏幕都分配了一个单独的 pushKey。我也无法使用这些 pushKeys 访问代码部分中的数据库

{
  const user = firebase.auth().currentUser.uid;
  const db = firebase.database();

  const ref = db.ref('kullaniciBilgiler/'+`${user}`);
  
  
 
    ref.once('value', (snapshot) => {
       var data=snapshot.val();
      console.log(data)
      
    }, (errorObject) => {
      console.log('The read failed: ' + errorObject.name);
    });

  

   
 


export default class profile extends Component {
  constructor(props) {
    super(props);
    this.state = {
    };
  }



  render() {


    var box = [];

    for(let i = 0; i <= 5; i++){
  
      box.push(

        <View style={{flexDirection:'row'}}>
        <View style={styles.textBox}>
          <Text style={styles.textMid}>Soyisim</Text>
        </View>
        <View style={styles.inputView}>                     
            <TextInput                         
             onChangeText={(text)=>setList(text)}
              style={styles.input}
           />
        </View>    
        </View>
      
      )
    }
    return (
      <ScrollView>
      <SafeAreaView style={styles.container} >
       
        <View style={styles.profilePhoto}>
         <Image 
         style={{
          width: 100,
          height: 100,
          resizeMode: 'contain',
          marginTop:40
        }}
        source={
          require('../../image/logo.png')
        }
         
         />

        </View>
        <View style={styles.profileDetails}>
                  
    {box}
                    

   </View>
                  
          
      </SafeAreaView>
      </ScrollView>
    );    
  }
}
}

我无法获取推送分配的随机密钥,也无法访问数据库中的信息

[]

_handleSubmit = (values) => {


auth()
  .createUserWithEmailAndPassword(values.email, values.password)
  .then(() => {
    userId = firebase.auth().currentUser.uid;
    if (userId) {
      var database = firebase.database().ref('kullaniciBilgiler/').child(userId).push();
      database.set({
        name: values.name,
        surname: values.surname,
        email: values.email,

      }).then(() => console.log('okey'));
    }

这个注册码

【问题讨论】:

  • 数据库图片位于顶部链接
  • 您应该使用.set() 而不是.push(),这样所有用户信息都在用户UID 节点下。您能否将包含push()ing 数据的代码分享给 Firebase?
  • 当然,请一分钟
  • 您能否编辑您的问题并将其添加为文本以便于复制和解释?

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


【解决方案1】:

createUserWithEmailAndPassword创建新用户时,将所有用户数据设置在UID节点下,而不是推送新节点。这可以通过使用set() 而不是push() 来完成:

const userId = firebase.auth().currentUser.uid;

const database = firebase.database().ref('kullaniciBilgiler/' + userId)

database.set({
  name: values.name,
  surname: values.surname,
  email: values.email,
}).then(() => console.log('okey'));

// similarly use 'update()' when adding other fields later

当您查询用户的节点时,它现在应该返回一个像这样的对象:

{
  name: '',
  surname: '',
  email: '',
  age: 0,
  ...otherFields
}

您可以在documentation 中阅读有关更新数据的更多信息。

【讨论】:

  • 我使用 formik-yup
  • 不幸的是我无法解决问题,因为正如我上面所说,我有一个由 3 个不同屏幕组成的记录系统,而不是添加它,它只删除并打印该页面上的信息
  • @TuranBicav 您应该使用update() 之后添加新字段,以免删除现有数据。你能用你用来更新其他字段的代码来更新你的问题吗?
  • 非常感谢,3天我怎么没想到。不知道是不是在生自己的气。
猜你喜欢
  • 2017-07-01
  • 2021-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多