49
rated 0 times
[
56]
[
7]
/ answers: 1 / hits: 25626
/ 6 Years ago, tue, june 12, 2018, 12:00:00
I have an issue with the Array.prototype.find()
method. It doesn't seem to work properly when it is used in $http request..
I've tried everything that came to mind but it always returns undefined. I guess the bug is there because I logged response.data
and user_to_find
before defining found_user
and their values are as expected.
So I have this service:
app.service('UserService', ['$http','$q', function($http, $q) {
return {
findUser: function(user_to_find) {
let defer = $q.defer();
$http({
method: GET,
url: 'http://localhost:3000/users'
}).then(function(response){
let found_user = response.data.find(acc=>{ //found_user is always undefined
acc.username == user_to_find;
})
defer.resolve(found_user);
}),function(response) {
defer.reject(response);
console.log(Error finding user);
}
return defer.promise;
}
}
}])
More From » angularjs