首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从firebase中获取嵌套子对象的数据?

无法从firebase中获取嵌套子对象的数据?
EN

Stack Overflow用户
提问于 2019-03-01 18:00:21
回答 2查看 37关注 0票数 0

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

数据库:

代码语言:javascript
复制
 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);

 });
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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

代码语言:javascript
复制
users-reviews: 
 - reviewingUserId 
   - comment: 'Wow'
   - rating: 5
   - subject: 'This may help you out'
票数 1
EN

Stack Overflow用户

发布于 2019-03-12 12:25:14

代码实际上是工作的,问题是我从循环内部调用了另一个函数"calculate_post_rating(post_id)",如下所示:

这是他们中的一些人跳过了计算的评级。

代码语言:javascript
复制
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函数是异步的,您永远不应该从循环中调用外部函数?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54942187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档