2016-08-22 24 views
0

Firebaseパスをフィルタリングして、自分のパスに子が存在するかどうか確認したいと思います。
私はちょうど "reponses"というパス呼び出しを持つ値を返したいと思います。それは動作しますが、パスがこの子を持っていないとき、それは私に、このエラーをスロー:
私はそのようなFirebase-utilのとAngularfire使用フィルタ関数の戻り値のAngularJsエラー

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 


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); 

私はどのように行うことができますreturn data.reponses.$value !== null;以外の適切な方法でテストしますか?これは、あなたが$valueのためにそれを調べる前にresponsesフィールドがdataであることを確認します

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

:ので、あなたがこのような$valueをチェックすることができ、あなたに

+2

子供の存在を確認して返さないと(data.responses && data.responses。$ value!== null)? – mhodges

答えて

2

ありがとうdata.responsesundefinedあるようですね。

+0

2番目の解決策はまだ私に同じエラーを返すが、最初の1つは仕事をする、あなたの答えをありがとう@Zach –

+0

私は2番目のコード部分を削除するまで、この答えをupvoteできません。あなたの最初のコードブロックは私がコメントに示唆したもので、うまくいくはずです。しかし、2番目の例では、bool && stringを返すほうが、直感的ではない結果を出すことがよくあります。論理演算子に関するドキュメントはこちらhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators – mhodges