Currently, my goal is to extract an object from a function. By utilizing an Angular JS promise, I am able to successfully log the availableProviders
once they have finished loading, and this information is being displayed correctly in my console.
function getServiceProviders(serviceId) {
var serviceProviders = ref.child('services').child(serviceId).child('providers');
var providers = ref.child('providers');
serviceProviders.on('value', function(snapshot) { // on services.serviceId.providers
var availableProviders = {}; // create empty availableProviders array
snapshot.forEach(function(childSnapshot) { // for each provider in services.serviceId.providers
var key = childSnapshot.key(); // grab each provider's key
providers.on('value', function(snap) { // on providers
if (snap.hasChild(key)) { // if providers has a child that matches the var key above
var item = snap.child(key); // store that child in a var called item
availableProviders[item.key()] = item.val(); // add item to availableProviders array
}
});
}); // rinse and repeat
var defer = $q.defer();
defer.promise
.then(function() {
console.log(availableProviders);
})
defer.resolve();
});
return availableProviders;
}
The issue arises when I attempt to have the getServiceProviders()
function provide these availableProviders
, resulting in an error indicating that`availableProviders` is not defined outside of the function scope.
ReferenceError: availableProviders is not defined
I am seeking advice or guidance on how to work around this obstacle. Any assistance would be greatly appreciated. Thank you in advance!