Am trying to recover the pure data from the call of the function regresarUsr(), am able to log the data with the function log but am not able to get the pure data back to the controller from inside the function regresarUsr() and then add to the $scope.user Always that i console.log() the data from the controller i get undefined, but i need the actual object.
app.factory('MGAindexdb', function(jsonService) {
//
// App global database instance and schema
//
var db = new Dexie("MGADT");
db.version(1).stores({
// friends: "++id,name,shoeSize",
users: "++id,usr_id,nombre,email,fb_email,registration_date,validated,membership,amount,last_access,key,is_logged_in,imagenDeUsuario,total,meQueda,gastoDia",
categories: "++id,category_id,user_id,nameCat,desc",
expenses: "++id,exp_id,user_id,cat_id,amount,comments,date"
});
db.open();
//
// Populate some data
//
function populateUser(u, c, e) {
log("Populating some populateUser", "heading");
return db.transaction("rw", db.users, db.categories, db.expenses, function () {
var cat;
var exp;
u = u.data;
// console.log(u);
db.users.clear();
db.categories.clear();
db.expenses.clear();
var img = u.imagenDeUsuario.split('"')[7];
console.log('Hola :DDD');
db.users.add({ usr_id: u.user_id, nombre: u.name, email: u.email, fb_email: u.fb_email, registration_date: u.registration_date, validated: u.validated, membership: u.membership_type, amount: u.amount, last_access: u.last_access, key: u.key, is_logged_in: u.is_logged_in, imagenDeUsuario: img, total: u.total, meQueda: u.meQueda, gastoDia: u.gastoDia});
var path = 'categories';
var categories = jsonService.getjson( c, path, e).then(function(d) {
cat = d.categories;
angular.forEach(cat, function(c) {
db.categories.add({category_id: c.category_id, user_id: c.user_id, nameCat: c.name, desc: c.description});
});
});
var path = 'expenses';
var expenses = jsonService.getjson( c, path, e).then(function(d) {
exp = d.expenses;
angular.forEach(exp, function(e) {
db.expenses.add({ exp_id: e.expense_id, user_id: e.user_id, cat_id: e.category_id, amount: e.amount, comments: e.comments, date: e.date});
});
});
// Log data from DB:
// db.users.orderBy('nombre').each(function (user) {
// log(JSON.stringify(user));
// });
// db.categories.orderBy('nameCat').each(function (cate) {
// log(JSON.stringify(cate));
// });
// db.expenses.orderBy('exp_id').each(function (expe) {
// log(JSON.stringify(expe));
// });
}).catch(function (e) {
log(e, "error");
console.log('Error de populateUser');
});
}
function regresarUsr () {
log("Regresando Data de Usr", "heading");
return db.transaction("rw", db.users, function () {
db.users.orderBy('nombre').each(function (user) {
// console.log(user);
// log(JSON.stringify(user));
// console.log(user);
// alert(Dexie.getByKeyPath(users, "nombre")); // Will alert ("Kalle");
return user;
});
}).catch(function (e) {
log(e, "error");
console.log('Error de regresarUsr');
});
}
//
// Helpers
//
function log(txt, clazz) {
var li = document.createElement('li');
li.textContent = txt.toString();
if (clazz) li.className = clazz;
document.getElementById('log').appendChild(li);
}
return {
open: open,
populateUser: populateUser,
regresarCat: regresarCat,
regresarExp: regresarExp,
regresarUsr: regresarUsr,
log: log
};
// End
});
app.controller('DashboardCtrl', function($http, $scope, jsonService, sesionesControl, MGAindexdb) {
$scope.user= function() {
// console.log(MGAindexdb.regresarUsr());
MGAindexdb.regresarUsr().then(function(data) {
console.log(data);
})
.catch(function (e) {
log(e, "error");
});
}
});
This is occuring because you are logging the factory return value before it has returned from the server, so it is undefined. You have to use a promise to wait until the factory actually contains data. In angular, you create a promise using the $q service.
app.factory('MGAindexdb', function (jsonService, $q) {
return {
regresarUsr: function () {
var deferred = $q.defer();
db.transaction("rw", db.users, function () {
db.users.orderBy('nombre').each(function (user) {
deferred.resolve(user);
});
}).
catch (function (e) {
deferred.reject(e);
});
return deferred.promise;
}
}
}
Now when you bind $scope.user to MGAindexdb.regresarUsr, a placeholder called a promise will be assigned to $scope.user, and it will resolve automatically when the server request returns.
Since angular 1.2, promises don't resolve automatically when assigned to scope variables. So you have to use Service.asyncCall().then(resolve promise here)
to manually resolve the promise.