i am using angular resource sails.
var items = sailsResource('roles').query(); // GET /item
$scope.roles = items;
angular.forEach($scope.roles, function(value, key) {
console.log(key + ': ' + value);
});
output: undefined.
How to parse this query?
Check this part of the documentation: https://github.com/angular-resource-sails/angular-resource-sails#success-and-error-callbacks
If you want to access the data you fetched, you'll probably have to provide the query function with callbacks. So your code would become
sailsResource('roles').query(function(items) { // GET /item
$scope.roles = items;
angular.forEach($scope.roles, function(value, key) {
console.log(key + ': ' + value);
});
});
The query
method is asynchronous. sailsResource
creates $resource
API compatible services so you have to do your looping in a callback function.
For example
$scope.roles = sailsResource('roles').query(function(roles) {
angular.forEach(roles, function(value, key) {
// and so on
});
});
You can also use the $promise
property to access the promise, eg
$scope.roles = sailsResource('roles').query();
$scope.roles.$promise.then(function() {
angular.forEach($scope.roles, function(value, key) {
// etc
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With