EDIT: The function is asynchronous so console.log was fired before the data was returned. I'll be putting my logic inside this .done()
function now.
I'm trying to take the results array and pass it into a global variable but the variable is logging as undefined outside of the results function.
It is however the array I need INSIDE the function.
I need to have an array of objects OUTSIDE of the results function as I cannot debug inside this function and have alot of code to write
Here is the code
$scope.syncContacts = function () {
var table = AzureService.getTable('contact');
var contactList = [];
table.read().done(function (results) {
contactList = results;
for (var i = 0; i < results.length; i++) {
console.log("Contact Object:", results[i]);
}
console.log('contact list is : ', contactList);
});
console.log("OUTSIDE FUNCTION LIST", contactList);
};
I have been reading around and have seen a function response.send(200, results)
being used in azure tables but have no idea what its used for or if I need it.
There's a variable scope issue wth contactList
, you are print the outside variable, and creating a new one inside the promise to populate.
var table = AzureService.getTable('contact');
table.read().done(function (results) {
$scope.contactList = results;
for (var i = 0; i < results.length; i++) {
console.log("Contact Object:", results[i]);
}
console.log('contact list is : ',$scope.contactList);
});
Here's the oficial documentation for AzureService JavaScript: Add Mobile Services to an existing app
EDIT:
The contactList
is being printed undefined because of the promise resolution. The done
method will take some time to get the data from the server. But, if you print it inside the done
, It will show up the result, as expected. You should put this value inside a $scope
variable, then you can use It after the read returns the data from the server:
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments