我正在尝试在我的莫纳卡应用程序上实现自动登录。为此,我使用 Web sql 数据库。手动登录后,我存储用户电子邮件和密码。在后续的创业过程中, 我在主 Controller 中调用我的服务 - 'dbTransaction' 并使用 dbTransaction.getUser()。 这成功地将电子邮件和密码存储到名为配置文件的全局变量中。
问题就在这里。当我在解决 promise 后提醒 profile.email 时,它将显示在 monaca IDE 上,但不会显示在我的 iPhone 上的 monaca 调试器上。我的手机上没有任何显示,登录功能也无法运行。请并感谢您的帮助。
app.service('dbTransaction', function($rootScope, $q){
var db;
this.Db = function(){
return window.openDatabase('database', "1.0", "DB", 100);
};
this.errorCb = function(err){
console.log("Error occured while executing SQL: "+err.code);
};
this.saveUser = function(email, password){
var self = this;
db = self.getDb();
db.transaction(function(tx){
tx.executeSql('CREATE TABLE IF NOT EXISTS user_table (email unique, password)');
tx.executeSql('INSERT INTO OR REPLACE INTO user_table (email, password) VALUES ("' + email + '", "'+ password + '")');
alert("wrote " + email + " " + password + " " + 'INSERT INTO user_table(email, password) VALUES ("' + email +'", "' + password + '")');
});
};
this.getUser = function(){
var self = this;
db = self.getDb();
var deferred = $q.defer();
setTimeout(function(){
db.transaction(function(tx){
tx.executeSql('CREATE TABLE IF NOT EXISTS user_table (email unique, password)');
tx.executeSql('SELECT * FROM user_table', [],
function(tx,result){
var loginInfo = [];
var len = result.rows.length;
if(len > 0){
//profile is a global variable
profile.email = result.rows.item(len-1).email;
profile.password = result.rows.item(len-1).password;
$rootScope.$apply(function(){deferred.resolve('worked');
alert(profile.password); //this successfully returns password
});
}else{
return;
}
}, function(error){
alert(3);
deferred.reject('something went wrong!');
});
});
}, 1000);
return deferred.promise;
};
});
主 Controller
app.controller('main', function($scope, $rootScope, $http, dbTransaction){
ons.ready(function() {
var promise = dbTransaction.getUser();
if(promise){
promise.then(function(){
showLoading();
var param = {
email:profile.email,
pass: profile.password
};
alert(param.email)//this successfully returns email
$http({
method: 'POST',
url: serverUrl + 'login.php',
data:param
}).success(function(data,status,headers,config){
alert(data);
if(data.result == 1 ){
hideLoading();
alert('login okay');
$rootScope.loggedIn = true;
isLoggedIn = true;
}else{
hideLoading();
alert(data);
}
});
});
}
});
});
最佳答案
我的sql语句写错了。
应该是
tx.executeSql('INSERT OR REPLACE INTO user_table
而不是
tx.executeSql('INSERT INTO OR REPLACE INTO user_table
关于javascript - 数据库事务的 promise 正在解决,但之后的函数未运行。 AngualarJS, Cordova ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35353888/