从 Firebase 中检索表格内的数值应如何操作?

huangapple go评论65阅读模式
英文:

How to retrieve a value from firebase within a table?

问题

以下是您要翻译的代码部分:

var database = firebase.database().ref().child('Datah/Real');
database.orderByChild("result").equalTo("RUNNING").once('value', function(snapshot){
    var content = '';
    snapshot.forEach(function(data){
        var key = data.ref.getKey();
        var time = data.val().time;
        var quote = data.val().quote;
        var type = data.val().type;

        var date = new Date(time * 1000);

        var current = 0;
        firebase.database().ref().child("Datah/Prices/" + quote).once("value", function(snapshot) {
            console.log(snapshot.key + " : " + snapshot.child("price").val());
            current = snapshot.child("price").val();
        });

        content += '<tr>';
        content += '<td>' + date.toLocaleString() + '</td>';
        content += '<td>' + quote + '</td>';
        content += '<td>' + type + '</td>';
        content += '<td>' + current + '</td>';
        content += '</tr>';
    });
});
$('#ex-table').append(content);
var tbody = $('table tbody');
tbody.html($('tr', tbody).get().reverse());

希望这有帮助!如果您有任何其他问题,请随时提出。

英文:

Let me start by showing what I have and what I have tried so far:

var database = firebase.database().ref().child(&#39;Datah/Real&#39;);
    database.orderByChild(&quot;result&quot;).equalTo(&quot;RUNNING&quot;).once(&#39;value&#39;, function(snapshot){
        var content = &#39;&#39;;
        snapshot.forEach(function(data){
          var key = data.ref.getKey();
          var time = data.val().time;
          var quote = data.val().quote;
          var type = data.val().type;

          var date = new Date(time * 1000);

          var current = 0;
          firebase.database().ref().child(&quot;Datah/Prices/&quot; + quote).once(&quot;value&quot;, function(snapshot) {
            console.log(snapshot.key+&quot; : &quot;+snapshot.child(&quot;price&quot;).val());
            current = snapshot.child(&quot;price&quot;).val();
          });

          content += &#39;&lt;tr&gt;&#39;;
          content += &#39;&lt;td&gt;&#39; + date.toLocaleString() + &#39;&lt;/td&gt;&#39;;
          content += &#39;&lt;td&gt;&#39; + quote + &#39;&lt;/td&gt;&#39;;
          content += &#39;&lt;td&gt;&#39; + type + &#39;&lt;/td&gt;&#39;;
          content += &#39;&lt;td&gt;&#39; + current + &#39;&lt;/td&gt;&#39;;
          content += &#39;&lt;/tr&gt;&#39;;
        });

        });
        $(&#39;#ex-table&#39;).append(content);
        var tbody = $(&#39;table tbody&#39;);
        tbody.html($(&#39;tr&#39;,tbody).get().reverse());
       });

I have data in firebase which I am retrieving from the first ref: Datah/Real, and this data contains a string value under quote, For each data I am retrieving I want to use quote to retrieve related data from another ref: Datah/Prices. In that ref I want to retrieve only data from a child equal to quote, therefore: Datah/Prices/quote. When I get this value I want to populate my table with it.

I managed to get first data and it is populating my table correctly, the challenge is on the second snapshot which should return current, because it still returns 0 as predefine, however on the console: console.log(snapshot.key+&quot; : &quot;+snapshot.child(&quot;price&quot;).val()); It is showing that it is actually retrieving the value, but why on my table current is always 0 and not 0 on console?

答案1

得分: 1

这是因为代码以异步方式运行。

此代码正确获取当前价格,但只有在代码的其余部分运行之后才能获取。

firebase.database()
    .ref("Datah/Prices/" + quote)
    .once("value", snapshot => {
        // 这段代码是异步的,也就是只有在返回此快照之后才运行,
        // 这将在代码中的“content =”行很久之后才会发生
        console.log(snapshot.key + " : " + snapshot.child("price").val());
        current = snapshot.child("price").val();

        // 因此,尝试将所有`content +=`的代码放在这里。
        content += '<tr>';
        content += '<td>' + date.toLocaleString() + '</td>';
        content += '<td>' + quote + '</td>';
        content += '<td>' + type + '</td>';
        content += '<td>' + current + '</td>';
        content += '</tr>';
        $('#ex-table').append(content);
    });

为了解决这个问题,可以考虑将所有表格更新移到“current = snapshot...”之后,这意味着它必须位于异步代码块内,就像我上面展示的那样。

您可能仍然需要对代码进行一些调整,以使jQuery更新发生 - 我不熟悉jQuery,所以我可能没有完全正确地将代码移到块内。

英文:

This is because the code runs asynchronously

This code is correctly getting the current price, but only AFTER the rest of the code has run.

firebase.database()
.ref(&quot;Datah/Prices/&quot; + quote)
.once(&quot;value&quot;, 
      snapshot=&gt;{
          // This code is asynchronous, i.e. only runs after this snapshot has been returned, 
          // which will be long after the &quot;content =&quot; lines in your code
              console.log(snapshot.key+&quot; : &quot;+snapshot.child(&quot;price&quot;).val());
          current = snapshot.child(&quot;price&quot;).val();


          // Therefore try putting all the `content += ` code in here.
          content += &#39;&lt;tr&gt;&#39;;
          content += &#39;&lt;td&gt;&#39; + date.toLocaleString() + &#39;&lt;/td&gt;&#39;;
          content += &#39;&lt;td&gt;&#39; + quote + &#39;&lt;/td&gt;&#39;;
          content += &#39;&lt;td&gt;&#39; + type + &#39;&lt;/td&gt;&#39;;
          content += &#39;&lt;td&gt;&#39; + current + &#39;&lt;/td&gt;&#39;;
          content += &#39;&lt;/tr&gt;&#39;;
          $(&#39;#ex-table&#39;).append(content);
      }
);

To solve this, how about moving all the table updates to AFTER the "current = snapshot...", which means it must be INSIDE the asynchronous code block, as I show above.

You might still need to fiddle with the code a bit to make the jQuery updates happen - I am not familiar with jQuery so I might not have moved exactly the right code inside the block.

huangapple
  • 本文由 发表于 2023年2月26日 20:45:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定