Javascript 无法从firebase获取嵌套子级的数据?
我正在尝试用一个特定的ID来获取该帖子的所有评论。我已经尝试了所有的解决方案,但都不起作用。当我试图获取posts 1级节点时,同样的方法也可以工作,但是嵌套的级别不起作用 db:Javascript 无法从firebase获取嵌套子级的数据?,javascript,firebase,ionic-framework,firebase-realtime-database,ionic2,Javascript,Firebase,Ionic Framework,Firebase Realtime Database,Ionic2,我正在尝试用一个特定的ID来获取该帖子的所有评论。我已经尝试了所有的解决方案,但都不起作用。当我试图获取posts 1级节点时,同样的方法也可以工作,但是嵌套的级别不起作用 db: 如果您以不同的方式构造数据并避免在密钥中使用密钥,则会更容易: 代码实际上正在运行,问题是我在循环中调用了另一个函数calculate_post_ratingpost_id,如下所示: 也就是说,他们中的一些人跳过了计算评级 let dbref = firebase.database().ref('/posts/'
如果您以不同的方式构造数据并避免在密钥中使用密钥,则会更容易:
代码实际上正在运行,问题是我在循环中调用了另一个函数calculate_post_ratingpost_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函数是异步的,因此永远不应该从循环中调用外部函数?您确定要传递的post\u id是正确的并且确实存在吗?乍一看,代码看起来是正确的。查看应用程序的日志输出是否有任何错误。如果这没有帮助,那么看看你是否可以在jsbin或stackblitz这样的工具中重现有或没有离子的问题,这样我们就可以看一看了。
users-reviews:
- reviewingUserId
- comment: 'Wow'
- rating: 5
- subject: 'This may help you out'
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
});