badigard
badigard

Reputation: 850

How to query Firebase denormalized data with Angularfire?

Following Firebase official docs I structured my data like this:

 {
  "contests": {
    "1": {
      "start_time": "2016-01-07T05:46:27 -02:00",
      "end_time": "2016-08-15T02:00:57 -03:00",
      "title": "The Best Voice ever!",
      "category": "music",
      "videos": {
        "video_id_44": true
      },
      "users": {
        "lgraves": true
      }
    },
    ....
  },
  "users": {
    "lgraves": {
      "p_name": "Lorie",
      "l_name": "Graves",
      "email": "[email protected]",
      "location": "Montana",
      "image_url": "https://randomuser.me/api/portraits/men/85.jpg",
      "created_time": "2014-12-22T03:40:38 -02:00",
      "videos": {
        "video_id_44": true
      }
    },
    ....
  },
  "videos": {
    "video_id_44": {
      "video_url": "www.cdn.video.com/9d0c60af-a069-484e-987f-fad9935dc9a7",
      "users": {
        "fhenderson": true
      }
    }, ...
  }
}

My Controller:

var vm = this;
var contestsRef = firebase.database().ref().child("contests");
vm.contests = $firebaseArray(contestsRef);      // synchronize the object with a three-way data binding

contestsRef.on("value", function(snapshot) {
        var key = snapshot.key();  /// error: snapshot.key is not a function
        var childKey = snapshot.val().user;
        ref.child("users/" + childKey + "/name").once('value', function(snapshot) {
        });
      });

When iterating with ng-repeat on contests I get error: FIREBASE WARNING: Exception was thrown by user callback. TypeError: snapshot.key is not a function

My question is: how do I access the 'users' and 'videos' when iterating over 'contests' array and still being synced on any changes done to 'contests' array?

Upvotes: 0

Views: 95

Answers (1)

Todd Hale
Todd Hale

Reputation: 571

Javebratt (Jorge) says try removing the parentheses from key():

var key = snapshot.key; // not snapshot.key()

Upvotes: 1

Related Questions