0
I have a service at the angle and wanted to do a foreach to recover the user value, to authenticate the login
My service:
.service('usuariosService', function ($rootScope, $location,$http) {
this.validaLogin = function(user){
var usuarios = [];
$http.get('api/user').then(function(resultado){
usuarios = resultado.data;
});
angular.forEach(usuarios, function(value, index){
if(value.nome == user.username){
$rootScope.usuarioLogado = value;
}else{
console.log('Não é igual');
}
})
}
})
But I can’t do this foreach, I can never buy the values, I don’t know if it’s the return of the api, or value.name isn’t getting any value.
my json returns this:
[{ "nome" : "teste", "password" : "teste" },
{ "nome" : "teste1", "password" : "teste1" }];
Anyone can help?
Try to get only the result and not result.data, from a console.log(result); and see the output.
– Nelson Aguiar
I gave a console log outside the $http block and came empty :( " [] " Inside the $http block came like this: Object {data: Array[1], status: 200, config: Object}
– Yuri Rodrigues
Dude is returning ok, what is the result content.data? Have that data there. And that’s right you have to run this in the callback of http.get, did you ever see the "date" content in "result"? if not there, you have to see what is coming in the function parameter that "user".
– Nelson Aguiar