简体   繁体   English

React Native Firebase DataSnapshot

[英]React Native Firebase DataSnapshot

I'm trying to implement firebase into my React Native app with the following code: 我正在尝试使用以下代码将firebase实现到我的React Native应用程序中:

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();
  });
});

I am getting the error: "undefined is not a function (evaluating 'rootRef.ref("items")') 我收到错误:“undefined不是一个函数(评估'rootRef.ref(”items“)')

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? 我是否使用React native的正确语法?

In firebase.database().ref , ref is a function so should be ref() . firebase.database().refref是一个函数,所以应该是ref()

Since that returns a Reference , you have to call child() to get a child. 因为它返回一个Reference ,你必须调用child()来获取一个孩子。

So: 所以:

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();
   });
});

I also renamed the query variable, because it's actually not a query but just another reference to a child location. 我还重命名了query变量,因为它实际上不是查询,只是对子位置的另一个引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM