Using Firebase-util to resolve one to many relationship

前端 未结 1 416
深忆病人
深忆病人 2020-12-20 09:00

I\'ve been looking at the firebase util project at https://firebase.github.io/firebase-util/. I can\'t figure out if this is possible.

Say I have a data set like th

相关标签:
1条回答
  • 2020-12-20 09:13

    You cannot use a nested list of groups as the keymap for clients/$client/groups. It must be a string pointing to exactly one reference.

    The solution would be to retrieve the list separately from the meta data for the user:

    var fb = new Firebase(URL);
    
    getClients(fb.child('clients'), function(userId, data) {
       console.log(userId, data);
    });
    
    function getGroupsForClient(clientId, callback) {
       var indexRef = fb.child('clients/'+clientId+'/groups');
       var groupsRef = fb.child('groups');
       var intersectionRef = new Firebase.util.intersection(indexRef, groupsRef);
       intersectionRef.once('value', callback);
    }
    
    function getClients(callback) {
       fb.child('clients').on('child_added', function(snap) {
          var userId = snap.name();
          var userData = snap.val();
          getGroupsForClient(userId, function(snap) {
             userData.groups = snap.val();
             callback(userId, userData);
          });
       });
    }
    
    0 讨论(0)
提交回复
热议问题