Pablo DelaNoche Pablo DelaNoche - 6 months ago 21
AngularJS Question

AngularJs error in return value from filter function

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

I just want to return the values wich have a path call "reponses". It works but when the path doesn't have this Child, it throw 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 !
$scope.items = $firebaseArray(nc);

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


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;

This makes sure that there is a responses field on data before you probe it for $value. You might even be able to get away with just

filter(function (data, key, priority) {
    return data.responses && data.responses.$value;

if you just looking for objects that have a defined responses and $value field.