【问题标题】:Receiving null from childSnapshot.key从 childSnapshot.key 接收 null
【发布时间】:2019-03-07 20:31:37
【问题描述】:

这就是我的数据库的样子:

 -members
 --L_C3OzCYFevhXzT5xk9 (specific key for Chat Room)
 ---user1: 'test@gmail.com'
 ---user2: 'test1@gmail.com'

我正在尝试从 Firebase 接收此特定密钥并将其设置为状态。这就是我尝试这样做的方式:

const query = firebase.database().ref('/members').orderByKey().equalTo(email);
     query.once('value')
     .then(function(snapshot) {
       snapshot.forEach(function(childSnapshot) {
         var key = childSnapshot.key;
         this.setState({ChatID: key});
       });
     });
     console.log(this.state.ChatID);

但每次我在 console.log 中收到“null” 我做错了什么?

【问题讨论】:

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


    【解决方案1】:

    查询是异步的,并返回一个稍后解决的承诺。您的日志消息在该查询完成之前执行。您只能在 promise 解析并调用您的 then() 回调后使用查询结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多