最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to do joins on Firebase tables - Stack Overflow

programmeradmin0浏览0评论

I am trying to get data from two tables like (fetch all users and their details)

tableOne.on('users', function (snapshot) {
    userId = snapshot.val().userId; // line 1 (results like 1,2,3,4,5,6)
    anotherTable.child('userdetails').child(userId).once('value', function(mediaSnap) {
     // result // line 2
 });
});

but the problem is line 1 executes first for the 6 times and then line 2 that n times resulting in everytime looking for 'where user id is - 6'...isn't joins supported in Firebase?

Any help is apreciated

I am trying to get data from two tables like (fetch all users and their details)

tableOne.on('users', function (snapshot) {
    userId = snapshot.val().userId; // line 1 (results like 1,2,3,4,5,6)
    anotherTable.child('userdetails').child(userId).once('value', function(mediaSnap) {
     // result // line 2
 });
});

but the problem is line 1 executes first for the 6 times and then line 2 that n times resulting in everytime looking for 'where user id is - 6'...isn't joins supported in Firebase?

Any help is apreciated

Share Improve this question edited Sep 7, 2014 at 16:59 Frank van Puffelen 599k85 gold badges888 silver badges858 bronze badges asked Sep 7, 2014 at 15:50 LuckyyLuckyy 1,0314 gold badges15 silver badges31 bronze badges 8
  • 3 You should probably read this article: firebase.com/blog/2013-10-01-queries-part-one.html#join and consider using this library: firebase.github.io/firebase-util. Also note that on('users' is not a valid event. You're probably lookinig for on('value' or on('child_added'. – Frank van Puffelen Commented Sep 7, 2014 at 16:51
  • Frank, I was working on first link only but problem is they are fetching single details and I want to fetch all the details from first table and corresponding details from second table – Luckyy Commented Sep 8, 2014 at 6:08
  • second link looks better, will look if it can help me...thanks – Luckyy Commented Sep 8, 2014 at 6:09
  • I tried reproducing your problem based on the code you provided, but without success (after I change on('users' to on('child_added'). Can you update this jsbin so that it reproduces the problem? jsbin.com/zifeqi/1/edit – Frank van Puffelen Commented Sep 8, 2014 at 13:40
  • Thanks Frank that was wonderful effort done by you...yes I have changed the code ...pls see the console you will see the result set has same user id – Luckyy Commented Sep 8, 2014 at 15:35
 |  Show 3 more comments

2 Answers 2

Reset to default 10

Your code snippet has a nasty side-effect:

var userId;
tableOne.on('value', function (snapshot) {
    userId = snapshot.val().userId; // line 1 (results like 1,2,3,4,5,6)
    anotherTable.child('userdetails').child(userId).once('value', function(mediaSnap) {
        console.log(userId + ":" + mediaSnap.val().name);
    });
});

You're not declaring userId as a variable, which means that it becomes a global variable in JavaScript. And since the callback function executes asynchronously, there is a good chance that the global values will have changed by the time you need it.

The solution is simply to make userId a local variable of the callback function:

tableOne.on('value', function (snapshot) {
    var userId = snapshot.val().userId; // line 1 (results like 1,2,3,4,5,6)
    anotherTable.child('userdetails').child(userId).once('value', function(mediaSnap) {
        console.log(userId + ":" + mediaSnap.val().name);
    });
});

This will ensure that each value of userId is captured inside the function.

Solution for list join:

tableOne.orderByKey().on("value", function (snapshot) {
    //console.log(snapshot.val());
    snapshot.forEach(function (data) {
        tableTwo.once('value').then(function (info) {
            info = info.val();
        });
    });
});
发布评论

评论列表(0)

  1. 暂无评论