Firebase.once未首次加载页面时未运行回调

时间:2016-03-18 03:10:26

标签: ionic-framework firebase angularfire

我在我的Ionic应用程序上有一个视图,它只显示一个选择下拉菜单,然后提示用户输入一个数字。然后将该数字保存到里程对象中的特定用户对象,该键是选定的选项,值是输入的数字。这是控制器和视图的代码。

控制器:

// Get who is logged in
$scope.user = facebook.get();
// Array of airlines
var airRef = ref.child("airlines");
$scope.airlines = $firebaseArray(airRef);
console.log($scope.airlines);
$scope.selectedAir = {};
$scope.miles = {};
// Add program to user
$scope.addProgram = function () {
  if(jQuery.isEmptyObject($scope.user)) {
      var authData = ref.getAuth();
      var theUser = ref.child("users").child(authData.uid);
      var selected = {};

      theUser.child("miles").child($scope.selectedAir.name.$id).once("value", function(snapshot) {
          console.log("apples");

          var exist = snapshot.exists();
          if(!exist) {
              selected[$scope.selectedAir.name.$id] = $scope.miles.num;
              theUser.child("miles").update(selected);
              $state.go("app.saved");
          } else {
              var alertPopup = $ionicPopup.alert({
                title: 'Oops!',
                template: "You already created this airline! Go to the 'Add Ticket' page to add more points."
              });
              alertPopup.then(function(res) {
                console.log("You already created this airline! Go to the 'Add Ticket' page to add more points.");
              });
          }
      })

  } else {
      var theUser = ref.child("users").child($scope.user.id);
      var selected = {};

      theUser.child("miles").child($scope.selectedAir.name.$id).once("value", function(snapshot) {
          var exist = snapshot.exists();
          if(!exist) {
              selected[$scope.selectedAir.name.$id] = $scope.miles.num;
              theUser.child("miles").update(selected);
              $state.go("app.saved");
          } else {
              var alertPopup = $ionicPopup.alert({
                title: 'Oops!',
                template: "You already created this airline! Go to the 'Add Ticket' page to add more points."
              });
              alertPopup.then(function(res) {
                console.log("You already created this airline! Go to the 'Add Ticket' page to add more points.");
              });
          }
      })

  }
}

查看:

<ion-view view-title="Add a Program">
<ion-nav-buttons side="left">
<button class="button back-button buttons button-clear header-item" ng-click="goBack()">
  <i class="icon ion-ios-arrow-back"></i>
</button>
  </ion-nav-buttons>
<ion-content class="padding">
      <div class="list marginZeroA paddingTL width80">
          <p class="mainText"> </p>
          <div class="list">
              <label class="item item-input item-select">
                  <div class="input-label">

                  </div>
                  <select ng-model="selectedAir.name" ng-options="airline.$id for airline in airlines">

                </select>
            </label>
          </div>
          <label class="item item-input loginInputs">
              <input style="color:black"ng-model="miles.num" class="milesPoints" type="number" min="0" max="10000000"
                placeholder="30000" ng-click="revealInput(1)">
          </label>
          <button class="button button-outline button-calm loginButton
            saveTicket" ng-click="addProgram()" disabled> Add </button>
    </div>
 </ion-content>
 </ion-view>

当我加载到特定视图时,该应用程序工作正常,但如果我从通常的登录页面开始,它将无法正常工作。 如果我在这里提供了错误的子ID:

.child($scope.selectedAir.name.$id).once("value",

我会收到一个控制台错误告诉我它错了所以我知道它正在尝试运行,但是当传递正确的子ID时没有任何反应。没有回调,没有错误捕获。感谢

1 个答案:

答案 0 :(得分:1)

找到我的问题:

ref.once('value', ... )ref.update( ... )之类的Firebase函数不执行任何内部命令以及不会抛出错误(错误回调中捕获)时,很可能是您的应用程序由于Firebase.goOffline()调用,因此未与Firebase相关联。这是我的情况,我在另一个视图中调用了这个,当我切换视图Firebase.goOnline()从未被调用以重新打开连接。