Am having trouble to update a variable, am getting undefined when I can see the content, am trying to keep the value of the variable data[0]
outside the then()
function, and finally add to a $scope variable.
My code is this:
var user = function(){
var usuario;
var uno = MGAindexdb.regresarUsr().then(function(data){
console.log('Dentro de usr');
console.log(data[0]); // here I see the object with it's content
usuario = data[0]; // copy the value to usuario
console.log(usuario); // same data with the object
}).catch(function (e) {
log(e, "error");
});
console.log(usuario); // print undefined
}
user();
// This is how the content looks like in dev chrome tool:
// Object {usr_id: 45, nombre: "jose", email: "jose@gmail.com", fb_email: "jose@gmail.com", registration_date: "2014-05-31T18:03:33.000Z"…}
I read this article that explains how closures work and i think it wasn't exactly my case and also read this great post that explains javascript scope. But still can't figure out why the value is not been saved. Thanks in advance for your time.