响应本机Firebase DataSnapshot [英] React Native Firebase DataSnapshot
问题描述
我正在尝试使用以下代码在我的React Native应用中实现Firebase:
I'm trying to implement firebase into my React Native app with the following code:
import * as firebase from 'firebase'
var fireBaseconfig = {
apiKey: "MY KEY",
authDomain: "MY DOMAIN",
databaseURL: "MY URL",
storageBucket: "MY BUCKET",
};
var firebaseApp = firebase.initializeApp(fireBaseconfig);
var rootRef = firebase.database().ref;
var query = rootRef.ref("items");
query.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key;
var childData = childSnapshot.val();
});
});
我收到错误消息:未定义不是函数(正在评估'rootRef.ref("items"))
I am getting the error: "undefined is not a function (evaluating 'rootRef.ref("items")')
我遵循了包括官方文档在内的一些教程,但尚未找到解决方案.我在React Native上使用正确的语法吗?
I have followed several tutorials including the official documentation and haven't been able find a solution to this. Am I using the right syntax for React native?
推荐答案
在firebase.database().ref
中,ref
是一个函数,因此应为ref()
.
In firebase.database().ref
, ref
is a function so should be ref()
.
由于返回了Reference
,因此您必须调用child()
来生孩子.
Since that returns a Reference
, you have to call child()
to get a child.
所以:
import * as firebase from 'firebase'
var fireBaseconfig = {
apiKey: "MY KEY",
authDomain: "MY DOMAIN",
databaseURL: "MY URL",
storageBucket: "MY BUCKET",
};
var firebaseApp = firebase.initializeApp(fireBaseconfig);
var rootRef = firebase.database().ref();
var ref = rootRef.child("items");
ref.once("value").then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key;
var childData = childSnapshot.val();
});
});
我还重命名了query
变量,因为它实际上不是查询,而只是对子位置的另一个引用.
I also renamed the query
variable, because it's actually not a query but just another reference to a child location.
这篇关于响应本机Firebase DataSnapshot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!