英文:
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('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());
});
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+" : "+snapshot.child("price").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("Datah/Prices/" + quote)
.once("value",
snapshot=>{
// This code is asynchronous, i.e. only runs after this snapshot has been returned,
// which will be long after the "content =" lines in your code
console.log(snapshot.key+" : "+snapshot.child("price").val());
current = snapshot.child("price").val();
// Therefore try putting all the `content += ` code in here.
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);
}
);
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论