angularjsfirebasefirebase-realtime-databasefirebase-util

AngularJs error in return value from filter function


I would like to filter a Firebase path and see if a child exists in my path.

I just want to return the values which have a path call "reponses". It works but when the path doesn't have this Child, it throws me this error:

    firebase.js:283 Uncaught TypeError: Cannot read property '$value' of 
    firebase.js:276 FIREBASE WARNING: Exception was thrown by user callback.
    TypeError: Cannot read property '$value' of undefined

I use Firebase-util and Angularfire like that:

    var ref = firebase.database().ref();

    var nc = new firebase.util.NormalizedCollection(
    ref.child('accounts/' + $localStorage.accountId + '/demandes'),
    [ref.child('demandes'), 'widget2']).select('widget2.duree', 'widget2.derniere_modification', 'widget2.reponses', 'widget2.exp', 'widget2.ajout_le', 'widget2.localisation').
    filter(function(data, key, priority) {
      return  data.reponses.$value !== null; // This cause problems !
    }).ref();
    $scope.items = $firebaseArray(nc);

How can I do to test in a proper way other than return data.reponses.$value !== null;? Thank you


Solution

  • It sounds like data.responses is undefined so you could check for $value like this:

    .filter(function (data, key, priority) {
        return data.responses && data.responses.$value !== null;
    }).ref();
    

    This makes sure that there is a responses field on data before you probe it for $value.