我试着用一个特定的ID来获得所有的评论,我尝试了所有的解决方案,但是从来都不起作用。当我尝试获取“post”(第一级节点)时,同样的方法也适用,但嵌套的级别不起作用
数据库:

calculate_post_rating(post_id){
console.log('this post id: '+post_id);
let dbref = firebase.database().ref('/user-reviews/'+ post_id + '/');
dbref.on('child_added', function (data){
console.log(data.key); //console is not even printing this, seems like this part is not even executed.
console.log('rated by user: ' + data.val().rating);
});发布于 2019-03-06 01:16:01
如果以不同的方式构造数据并避免使用key It key,将会更容易:
https://firebase.google.com/docs/database/rest/structure-data#how_data_is_structured_its_a_json_tree
users-reviews:
- reviewingUserId
- comment: 'Wow'
- rating: 5
- subject: 'This may help you out'发布于 2019-03-12 12:25:14
代码实际上是工作的,问题是我从循环内部调用了另一个函数"calculate_post_rating(post_id)",如下所示:
这是他们中的一些人跳过了计算的评级。
let dbref = firebase.database().ref('/posts/');
dbref.on('child_added', function (data){
console.log(data.key); //console is not even printing this, seems like this part is not even executed.
console.log('rated by user: ' + data.val().rating);
let final_rating = calculate_rating(data.key); // this is where the expected final rating wasn't looping properly
});由此我可以得出结论:由于firebase函数是异步的,您永远不应该从循环中调用外部函数?
https://stackoverflow.com/questions/54942187
复制相似问题