Firebase 上未调用 ref.once()

Mic*_*hel 7 javascript firebase firebase-realtime-database

我对以下问题感到困惑。在制作一个小网页来查看 Firebase 上的一些数据时,在某些地方无法访问我想要的内容。\n当然,我知道服务器上的这个地方有一些东西。这是代码,似乎 localRef.once() 从未被调用。

\n\n
var dbLocalURL = "https://atinytestapp.firebaseio.com/MyList/rc99/";\nvar localRef = new Firebase(dbLocalURL);\nlocalRef.once("value", function(snapshot) {\n    // WE NEVER COME HERE !!!!\n    for (record in snapshot.val()) {\n        document.write(record)\n        var recordRef = new Firebase(dbLocalURL+record+"/");\n        recordRef.once("value", function(rcdSnapshot) {\n            var rcdData = rcdSnapshot.val();\n            document.write(rcdData[\xe2\x80\x9csomeKey\xe2\x80\x9d])\n        })\n    }\n}, function (errorObject) {\n    // WE NEVER COME HERE !!!!\n    console.log("The read failed: " + errorObject.code);\n    document.write("The read failed: " + errorObject.code);\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

我访问数据的页面在任何地方都能正常工作,除了一个具有上述行为的地方。\n此外,我还通过将其粘贴到浏览器中并查看我期望的数据来检查我使用的 URL 是否正确。\ n我可能会错过什么?我希望有人能给我提示。

\n

Jay*_*Jay 4

(注意:这是新的 Firebase 3 方式)这未包含在问题的代码中,但这是初始化(此处是为了完整性)

// Set the configuration for your app: Replace with your project's config object
  var config = {
    apiKey: '<your-api-key>',
    authDomain: '<your-auth-domain>',
    databaseURL: '<your-database-url>',
    storageBucket: '<your-storage-bucket>'
  };
  firebase.initializeApp(config);

  // Get a reference to the database service
  var database = firebase.database();
Run Code Online (Sandbox Code Playgroud)

假设您只想读取数据一次:

firebase.database().ref('MyList/rc99').once('value').then(function(snapshot) {
    for (record in snapshot.val()) { //unordered records, see comment
       document.write(record)
       ...
Run Code Online (Sandbox Code Playgroud)

火力基地 2

var ref = new Firebase("https://atinytestapp.firebaseio.com/MyList/rc99/");

ref.on("value", function(snapshot) {
  snapshot.forEach(function(record) { //ordered records, see comment
    document.write(record)
    ...
});
Run Code Online (Sandbox Code Playgroud)

注意:确保网址正确,如果格式错误,则根本无法使用。我尝试了你的网址,但似乎没有响应。