【发布时间】:2018-04-05 09:59:12
【问题描述】:
尝试使用 Firebase 在 React Native 中设置一些测试数据。我已使用$yarn add firebase 成功安装。我在 FB 中添加了这样的测试数据:
FB data
在我的项目中,我添加了以下代码:
import * as firebase from 'firebase'
const firebaseConfig = {
apiKey: "AIzaSyBNKM6Ptbynkg5dEJkwMHNsZhUCsW2JqGE",
authDomain: "testproject-f4c9f.firebaseapp.com",
databaseURL: "https://testproject-f4c9f.firebaseio.com",
projectId: "testproject-f4c9f",
storageBucket: "testproject-f4c9f.appspot.com",
messagingSenderId: "48530616964"
}
firebase.initializeApp(firebaseConfig)
然后在渲染中:
let mytext = ""
let testing =
firebase.database().ref('testCategory/test1/FirstHeader');
testing.on('value', function(snapshot) {
mytext = snapshot.val()
});
return(
<View>
<Text>{mytext}</Text>
</View>
);
运行应用程序,没有任何显示。任何想法将不胜感激。
更新:
我设法用这个代码在console.log 中得到它:
let child = ""
var ref = firebase.database().ref("testCategory");
ref.once("value")
.then(function(snapshot) {
child = snapshot.child("test1/testHeader").val()
console.log({child})
});
但由于某种原因,我无法在文本输出中打印出来:
return(
<View>
<Text>{this.child}</Text>
</View>
);
它只是空白......
【问题讨论】:
-
尝试放快照日志,检查是否有数据?
-
不,它是空的:/
-
现在可以在控制台上使用,但不能在文本视图中使用:请参阅更新。
标签: javascript firebase react-native firebase-realtime-database