Access a data inside a json object (no array)

1

I'm working with angularjs, I have a query to bring me a user by document number, I return a json with the data, what I need is to access the data idUsuario of that json object to inject it elsewhere, to give a console.log in the variable to which I assign the result of the query, shows this.

I have tried with vm.data.idUser, vm.data ["userID"], but it tells me that it is undefined, I work with javascript, what is the correct way to do it?.

this is the code

var vm = this;   
vm.user = {};  
vm.user = Usuarios.queryBydocuni({
                    query: data[i].documento //esta parte es para sacar el parámetro de consulta de otra parte

});
 vm.id = vm.user["idUsuario"];
console.log(vm.id);

Thanks in advance for your help.

    
asked by BastianBurst 28.08.2017 в 21:04
source

2 answers

2

Updated

What happens is that your current code tries to read about the vm.user object before the response arrives from the query, what you need is to wait for the response to arrive so you can assign the value to vm.user and later use it later, try to put your code like that and tell me:

var vm = this;   
Usuarios.queryBydocuni({
  // Esta parte es para obtener el parámetro de la consulta de otra parte
  query: data[i].documento
}).$promise.then(function(usr){
   vm.user = usr;
   vm.id = vm.user["idUsuario"];
   console.log(vm.id);
})
    
answered by 28.08.2017 / 21:38
source
0

First debug the object with console.log(JSON.stringify(data)) to see what you are receiving. Then simply in the place where you are receiving it, assign it to a variable.

let id;
id = data.json().idUsuario ;
return id// devolvería lo que solicitas.
    
answered by 28.08.2017 в 21:33