【问题标题】:Show Firebase data as Json in browser Console在浏览器控制台中将 Firebase 数据显示为 Json
【发布时间】:2018-06-08 15:30:39
【问题描述】:

我需要帮助才能将我的 Firebase 数据库中的数据以 JSON 格式显示到浏览器控制台日志中。 我只想将 Product 加载到浏览器控制台中。我找到了从this link 推送数据的代码。谁能帮我弄清楚这个功能吗?

var firebase = require('firebase');
//var admin = require('firebase-admin');

firebase.initializeApp({
  apiKey: "...",
  authDomain: "xxx.firebaseapp.com",
  databaseURL: "https://xxx.firebaseio.com",
  projectId: "xxx",
  storageBucket: "xxx.appspot.com",
  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: 'https://firebasestorage.googleapis.com/v0/b/ng-product.appspot.com/o/Uploads%2Ftest1.jpg?alt=media&token=f105332a-02c8-46ca-a639-745eda0e118c'

})*/

console.log('Product');

【问题讨论】:

  • json.stringify

标签: javascript json node.js firebase firebase-realtime-database


【解决方案1】:

要从 Firebase 获取数据,您需要附加一个侦听器。要将其打印到浏览器控制台,请在回调中调用 console.log()

var ref = firebase.database().ref('Product');
ref.once('value', function(snapshot) {
  console.log(snapshot.val());
});

我强烈建议您抽时间阅读Firebase Database documentation,并获取相应的codelab。在这些方面花费几个小时将节省更多时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-29
    • 2020-09-12
    • 2020-04-20
    • 2019-01-12
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多