Firebase数据库并做出本机反应

时间:2017-03-15 00:06:00

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

以下是数据库的示例结构。

root: {
  users: {
    user_uid_0: {
      name: 'AJ',
      age: 20,
      gender: 'male'
    }
  }
}

以下是我从Firebase获取数据的方式:

getData(myValue) {

  // direct ref to a key of 'name' from user_uid_0
  const name = firebase.database().ref('/users/user_uid_0/name');

  // name will go through a listener
  name.on('value', (snapshot) => {
    // define myValue to the snapshot
    myValue = snapshot.val()
    // this.myValue = snapshot.val() tried this...
  });

  // spit out myValue which we defined
  // myValue should be 'AJ'
  // but its not... its undefined
  return myValue;
  // im guessing because myValue is actually not defined...
  // then how do I define it with my snapshot.val()?
}

下面是一个反应本机组件,显示函数返回的内容。

// this should return the string 'AJ' from myValue
// this returns nothing
<Text>{this.getData()}</Text>

以下是我登录日志时的内容。

// undefined is what I get
console.log(this.getData());

因为这不会起作用,会是什么?如何通过ref获取数据并显示它?请帮忙。几天来我一直在努力。

1 个答案:

答案 0 :(得分:2)

尝试这样的事情 -

<receiver android:name=".IncomingSms"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>