简体   繁体   English

如果Firebase中不存在对象,如何显示其他数据?

[英]How to display other data if object doesn't exist in Firebase?

Alright, I am passing way too much time on this one, seems completely trivial but somehow I can not sort it out. 好吧,我传递了太多的时间在这一个,似乎完全是微不足道的,但不知何故,我不能把它整理出来。

I am trying to display the data from an object to my DOM : 我试图显示从对象到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";
    })
    }
    })
    });
  });

Now, if the snapshot42 is empty / has nothing, I want to redefine what I show. 现在,如果snapshot42为空/什么都没有,我想重新定义显示的内容。 It just doesn't work. 就是行不通。

Here is my DOM : 这是我的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>

Any solution ? 有什么办法吗?

Your problem may be that you attempt to access the value, before checking whether the value even exists. 您的问题可能是您在检查值是否存在之前尝试访问该值。 Ie you call snapshot42.val().name before calling snapshot42.exists() . 也就是说,在调用snapshot42.val().name之前,先调用snapshot42.exists()snapshot42.val().name If the value doesn't exist, snapshot42.val() will return null, which will throw an error saying Cannot read property 'name' of null once you try to access properties on it. 如果该值不存在,则snapshot42.val()将返回null,这将引发错误Cannot read property 'name' of null您尝试访问其属性时Cannot read property 'name' of null You havn't provided any information regarding any error messages though, so it's just a shot in the dark. 您没有提供有关任何错误消息的任何信息,因此这只是黑暗中的一枪。

Anyways, if the above is in fact the problem, the following changes would solve them: 无论如何,如果上述实际上是问题所在,则以下更改将解决它们:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM