我正在尝试在我的monaca应用程序上实现自动登录。为此,我使用web sql数据库。手动登录后,我存储用户电子邮件和密码。在随后的初创公司中, 我在我的主控制器中调用我的SERVICE - 'dbTransaction'并使用dbTransaction.getUser()。 这成功地将电子邮件和密码存储到名为profile的全局变量中。
这是问题所在。当我在解析承诺后警告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;
};
});
主控制器
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);
}
});
});
}
});
});
答案 0 :(得分:1)
我的sql语句写得不正确。
它假设是
tx.executeSql('INSERT OR REPLACE INTO user_table
而不是
tx.executeSql('INSERT INTO OR REPLACE INTO user_table