2017-03-18 5 views
0

申し訳ありませんが、私はこの1つにあまりにも多くの時間を渡している、完全に自明だが何とか私はできるそれを並べ替える。オブジェクトがFirebaseに存在しない場合、他のデータを表示する方法は?

私はDOMにオブジェクトからのデータを表示しようとしています:

// Listen to every people in our query... 
    geoQuery40.on("key_entered", function(key) { 
    // ... and look them up by key ... 
    restaurantsRef40.child(key).on("value", function(snapshot42) { 
     var restaurant40 = snapshot42.val(); 
     console.log(restaurant40); 
     var displayBusinessName = snapshot42.val().name; 
     console.log(displayBusinessName); 
     var displayBusinessDescription = snapshot42.val().description; 
     $timeout(function() { 
     if (snapshot42.exists()) { 
      $timeout(function() { 
     $scope.displayBusinessName = displayBusinessName; 
     $scope.displayBusinessDescription = displayBusinessDescription; 
    }) 
    } else { 
     $timeout(function() { 
     $scope.displayBusinessName = "displayBusinessName"; 
     $scope.displayBusinessDescription = "displayBusinessDescription"; 
    }) 
    } 
    }) 
    }); 
    }); 

は今、snapshot42が空の場合は/何もしています、私は私が示すものを再定義します。それだけでは機能しません。ここで

は私のDOMです:

<div class="content2" ng-controller="businessPageController"> 
    <div id="map"></div> 
    <div class="profile-info"> 
       <!-- *profile-image/image profile --> 
       <img class="profile-image" src="img/100.jpg"> 
       <!-- *profile-name/name profile --> 
       <h3 class="profile-name">{{displayBusinessName}}</h3> 
       <!-- *profile-description/description profile --> 
       <span class="profile-description"> 
        {{displayBusinessDescription}} 
       </span> 
     <br /><br /> 
       </div> 
      </div> 

任意のソリューション?

+0

ジャストヘッドアップ。 '.on( 'value'、[callback])を使うときは、実際に値変更イベントのリスナーを登録しています。したがって、key_enteredイベントが2回目で発生しなくても、コールバックが実行される可能性があります。 "key_entered"イベントがトリガされたときに値を一度だけ取得したい場合は、once( 'value'、[callback]) 'を使うか、より良いですが' once( 'value')を使用します。 )。 –

答えて

0

値が存在するかどうかを確認する前に、値にアクセスしようとした可能性があります。私。 snapshot42.exists()を呼び出す前にsnapshot42.val().nameに電話してください。値が存在しない場合、snapshot42.val()はヌルを返します。ヌルのプロパティにアクセスしようとすると、エラーはCannot read property 'name' of nullとなります。 エラーメッセージに関する情報は提供されていないので、暗闇の中でのショットです。上記実際に問題になる場合

とにかく、次の変更は、それらを解決するだろう:必ずしもあなたの問題に関連していない

geoQuery40.on("key_entered", function(key) { 
    // ... and look them up by key ... 
    restaurantsRef40.child(key).once("value", function(snapshot42) { 
     if(snapshot42.exists()){ 
      var restaurant40 = snapshot42.val(); 
      $scope.$apply(function(){ 
       $scope.displayBusinessName = restaurant40.name; 
       $scope.displayBusinessDescription = restaurant40.description; 
      }); 
     }else{ 
      $scope.$apply(function(){ 
       $scope.displayBusinessName = "displayBusinessName"; 
       $scope.displayBusinessDescription = "displayBusinessDescription"; 
      }); 
     } 
    }); 
}); 
関連する問題