Here is the code: I'm looping through the data to get the value that I need, and I got it. The questions is how can I access the value outside of this loop in other functions within the controller. The correct value have been assigned to $scope.theModel.
var refModels = firebase.database().ref("/users/" + $scope.UserID);
refModels.once('value').then(function (snapshot) {
snapshot.forEach(function (snapshot) {
var usersValue = snapshot.val();
console.log("users values", usersValue.carModel);
$scope.theModel = usersValue.carModel;
console.log("The model" + $scope.theModel); //output "e90"
});
$scope.processData();
});
So I need to use that value $scope.theModel outside of the function. What I'm trying to achieve, I will use the value of $scope.theModel -"e90" and compare it with another DB ref, and if it matches it will return the data. See the code of another DB ref:
$scope.processData = function (){
console.log("Process Model" + $scope.theModel); // returns undefined
$scope.carDetails = [];
firebase.database().ref("models").once('value').then(function(snapshot) {
snapshot.forEach(function(userSnapshot) {
var models = userSnapshot.val();
console.log(models);
if (models.brand == $scope.theModel){
$scope.carDetails.push(models);
}
});
});
};