vendredi 27 février 2015

Retrieve res var from $cordovaSQLite in cordova/phonegap

I'm trying to get data from a database but I don't know how to stock the result in a variable even if the variable is global. Here is my code:



.factory('Requirements', function($cordovaSQLite) {
return {
get: function() {
//Vérification de l'existance
var result = false;
query = "select id, instagram_client_id, instagram_client_secret, twitter_client_id, twitter_client_secret from requirements WHERE id = 1";
$cordovaSQLite.execute(db, query, []).then(function(res) {
if(res.rows.length == 0) {
query = "INSERT INTO requirements(id, instagram_client_id, instagram_client_secret, twitter_client_id, twitter_client_secret) VALUES (?,?,?,?,?)";
$cordovaSQLite.execute(db, query, [1,'','','','']).then(function(res) {
result = {"twitter_client_secret":"test","instagram_client_id":"","instagram_client_secret":"","twitter_client_id":""};
});
}else{
result = res.rows.item(0);
}
});
return result;
}
}
})


And I call my service that way:



requirements = Requirements.get();


But result from $cordovaSQLite.execute(db, query, []).then(function(res){}); isn't the same from the get fuction and I know that my code is executed because I've added console.log() and the app enter in the good part of my code but always return false.


So how can I return the good result please?


Aucun commentaire:

Enregistrer un commentaire