在浏览器控制台中将Firebase数据显示为Json
我需要帮助才能将我的Firebase数据库中的数据显示为JSON到浏览器控制台log.我只想将Product
加载到浏览器控制台中。我找到了一个从this link推送数据的代码。任何人都可以帮我弄清楚这个功能吗?
var firebase = require('firebase');
//var admin = require('firebase-admin');
firebase.initializeApp({
apiKey: "...",
authDomain: "xxx.firebaseapp",
databaseURL: "",
projectId: "xxx",
storageBucket: "xxx.appspot",
messagingSenderId: "xxx"
})
var ref = firebase.database().ref('Product');
//it's child directory
var messageRef = ref.child('/');
/*messageRef.push
({
prdName: 'node1',
prdCategory: 'node1',
prdSup: 'node1',
prdDescription: 'node1',
prdImage: 'test1.jpg',
prdUrl: '.appspot/o/Uploads%2Ftest1.jpg?alt=media&token=f105332a-02c8-46ca-a639-745eda0e118c'
})*/
console.log('Product');
回答如下:
要从Firebase获取数据,请附加一个侦听器。要将其打印到浏览器控制台,请在回调中调用console.log()
:
var ref = firebase.database().ref('Product');
ref.once('value', function(snapshot) {
console.log(snapshot.val());
});
我强烈建议你花一些时间阅读Firebase Database documentation,并采取相应的codelab。花费几个小时的时间可以节省更多的时间。