将firebase数据检索到Web

时间:2018-03-23 13:51:33

标签: javascript firebase firebase-authentication



// Initialize Firebase
  var config = {
    apiKey: "AIzaSyBz13eirmEOGD1uXOZwf6tKGnEsjfwsUFo",
    authDomain: "platformtechproject.firebaseapp.com",
    databaseURL: "https://platformtechproject.firebaseio.com",
    projectId: "platformtechproject",
    storageBucket: "platformtechproject.appspot.com",
    messagingSenderId: "1065758005439"
  };
  firebase.initializeApp(config);

  
  var database = firebase.database();

 var msgRef = firebase.database().ref('survey');
    
  
  //console.log(firebase);
  
var msgRef = firebase().ref('survey');
msgRef.on('value',gotData,errData);
	




我继续收到此错误[Uncaught TypeError:firebase不是函数],因为我正在尝试将firebase数据读取到Web。 Chrome console error

2 个答案:

答案 0 :(得分:0)

改变这个:

 var database = firebase.database();

var msgRef = firebase.database().ref('survey');


 //console.log(firebase);

var msgRef = firebase().ref('survey');
msgRef.on('value',gotData,errData);

到此:

 var database = firebase.database();
 var msgRef = database.ref('survey');
 msgRef.on('value', function(snapshot) {
  //retrieve data
 });

答案 1 :(得分:0)

这里是您的代码必须如何:

<script src="https://www.gstatic.com/firebasejs/4.8.0/firebase.js"></script>
<script>
// Initialize Firebase
  var config = {
    apiKey: "AIzaSyBz13eirmEOGD1uXOZwf6tKGnEsjfwsUFo",
    authDomain: "platformtechproject.firebaseapp.com",
    databaseURL: "https://platformtechproject.firebaseio.com",
    projectId: "platformtechproject",
    storageBucket: "platformtechproject.appspot.com",
    messagingSenderId: "1065758005439"
  };
  firebase.initializeApp(config);


  var database = firebase.database();

 var msgRef = database.ref('survey');


msgRef.on("child_added",function(snapshot) {
  var changedPost = snapshot.val();
  console.log(changedPost)
});
</script>

上面的代码按预期工作: enter image description here

请注意,msgRef会侦听这些预定义的&#34;事件&#34;。

详细信息:Read Event Types

此致