0

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.

Community
  • 1
  • 1
GabouhSk8
  • 159
  • 1
  • 13
  • It is an async operation, so `console.log(usuario);` runs before the promise then chain callback is run which sets the data to usuario. You must return promise from user function and let the consumers chain through to get the data. See [this](http://stackoverflow.com/questions/16855836/angularjs-http-get-then-and-binding-to-a-list) as well. – PSL Jan 20 '15 at 20:15
  • Am getting that data from a factory in services.js and am not using any promise to get that data – GabouhSk8 Jan 20 '15 at 20:23
  • Regardless, your question is more specific to accessing data from an async callback. Linked answer will give you more details on that. – PSL Jan 20 '15 at 20:24
  • app.factory('MGAindexdb', function (jsonService, $q) { var db = new Dexie("MGADT"); db.version(1).stores({ users: "++id,usr_id,nombre,email,fb_email,registration_date,validated,membership,amount,last_access,key,is_logged_in,imagenDeUsuario,total,meQueda,gastoDia" }); db.open(); return { regresarUsr: function () { return db.transaction("rw", db.users, function () { return db.users.orderBy('nombre').toArray(); }); } } }); And to get the data in usuario is the code posted before from the dashboard – GabouhSk8 Jan 20 '15 at 20:25
  • and I already try the solution proposed: .then(function(data) { $scope.usuario = data[0]; }) But still got undefined – GabouhSk8 Jan 20 '15 at 20:27
  • I would suggest you to read upon how to work with asyncronous operation, also the answer marked as duplicate has a very good explanation. Did you read the linked answer. when you say `console.log(usuario); // print undefined` yes because it will run before you even set it. – PSL Jan 20 '15 at 20:31
  • I just try this code too but give me error: Cannot read property 'then' of undefined user().then(function(bio){ $scope.usuario = bio[0]; console.log($scope.usuario); }); – GabouhSk8 Jan 20 '15 at 20:33
  • `then` is not a magic, you need to return promise from user function. ex:`return MGAindexdb.regresarUsr().then(function(data){ return data[0]; }` The other answer linked in my prev comment will provide insights on that. – PSL Jan 20 '15 at 20:34
  • yeah I did, and it's very possible that am printing before the asynchrous operation is over – GabouhSk8 Jan 20 '15 at 20:36
  • in fact i have already try something like what you said: var user = function(){ var usuario; var uno = MGAindexdb.regresarUsr().then(function(data){ console.log('Dentro de usr'); // console.log(data[0]); usuario = data[0]; return usuario; }).catch(function (e) { log(e, "error"); }); return uno; } – GabouhSk8 Jan 20 '15 at 20:39
  • and it console.log this: t {_state: null, _value: null, _deferreds: Array[0], _catched: false, _PSD: null…} – GabouhSk8 Jan 20 '15 at 20:40
  • @PSL I have read the links you post and read some others and i still can't get the value :/ I already use promise with defer and $q and still can't save the value from data[0] outside the function – GabouhSk8 Jan 20 '15 at 22:49

0 Answers0