React Native Firebase DataSnapshot

这一生的挚爱 提交于 2019-12-24 08:01:04

问题


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

I am getting the error: "undefined is not a function (evaluating '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?


回答1:


In firebase.database().ref, ref is a function so should be ref().

Since that returns a Reference, you have to call child() to get a 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.



来源:https://stackoverflow.com/questions/44191233/react-native-firebase-datasnapshot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!