Angular v.1.6.1
I am attempting to return a collection using $q.defer().promise, but I am only getting a single record instead of both records that are returned from my service.
The Model:
angular.module('app').factory('User',
function() {
/**
* Constructor, with class name
*/
function User(id, firstName, lastName, startDate) {
// Public properties, assigned to the instance ('this')
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.startDate = startDate;
}
/**
* Public method, assigned to prototype
*/
User.prototype.getFullName = function() {
return this.firstName + ' ' + this.lastName;
};
User.apiResponseTransformer = function (responseData) {
if (angular.isArray(responseData)) {
return responseData
.map(User.build)
.filter(Boolean);
}
return User.build(responseData);
}
/**
* Static method, assigned to class
* Instance ('this') is not available in static context
*/
User.build = function(data) {
return new User(
data.Id,
data.FirstName,
data.LastName,
data.StartDate
);
};
/**
* Return the constructor function
*/
return User;
});
The Service
(function () {
'use strict';
var serviceId = 'userService';
angular.module('app').factory(serviceId, ['common', '$http', 'config', 'User', userService]);
function userService(common, $http, config, User) {
var $q = common.$q;
var defer = $q.defer();
var service = {
getUsers: getUsers,
getUser: getUser
};
return service;
function getUser(id) {
$http({
method: 'get',
url: config.remoteServiceName + 'users/' + id
}).then(function (response) {
defer.resolve(User.apiResponseTransformer(response.data));
}).then(function (response) {
defer.reject(response);
});
return defer.promise;
}
function getUsers(startDate) {
$http({
method: 'get',
url: config.remoteServiceName +'users/',
params: {
startDate: startDate
}
}).then(function (response) {
var users = [];
angular.forEach(response.data, function (value, key) {
users.push(User.apiResponseTransformer(value));
});
defer.resolve(users);
}).then(function(response) {
defer.reject(response);
});
return defer.promise;
}
}
})();
The View Methods
function getUser() {
userService.getUser(1).then(function successCallback(data) {
vm.user = data;
}).catch(function () {
log('An error occured trying to get user...');
});
}
function getUsers() {
userService.getUsers(new Date().toUTCString()).then(function successCallback(data) {
vm.users = data;
}).catch(function () {
log('An error occured trying to get user...');
});
}
Inside the view, the getUser call functions as expteced but getUsers receives only the first item in the collection from the service. I have verified that response.data does contain the entire collection of objects and those objects are pushed into the users array.
Even calling defer.resolve(response.data) only sends the first item in the collection.
Any assistance is appreciated!