Reputation: 7927
I have a view on my Ionic app that simply displays a select dropdown menu, and then prompts the user to type in a number. The number is then saved to that specific user's object in the miles object, the key being the selected option and the value being the number typed in. Here is the code for the controller and view.
Controller:
// 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.");
});
}
})
}
}
View:
<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>
The app works perfect when I load into that specific view, but if I start from the login page as would normally be done it will not work. If I provide the wrong child id here:
.child($scope.selectedAir.name.$id).once("value",
I will get a console error telling me its wrong so I know it is trying to run, but when a correct child id is passed in nothing happens. No callback, no error catching. Thanks
Upvotes: 1
Views: 223
Reputation: 7927
Found my issue:
When a Firebase function such as ref.once('value', ... )
or ref.update( ... )
does not perform any of the inside commands as well as does not throw an error, which is caught in the error callback, then most likely your application is not connected to Firebase because of a Firebase.goOffline()
call. This was in my case and I called this inside another view and when I switched views Firebase.goOnline()
was never called to turn the connection back on.
Upvotes: 1