AngularJS javascript函数卡在else语句中
我有这个代码,我想检查代码输入数据在本地数据库中是否相同。这段代码工作正常,直到到达我标记为的地方,代码挂起或停止在这里。一旦代码达到条件,它就可以完美地运行,并将notifier赋值为true,但它没有从该函数中出来,因此代码的其余部分不会执行。谁能给我解释一下原因吗?我正在构建一个Ionic AngularJS应用程序AngularJS javascript函数卡在else语句中,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我有这个代码,我想检查代码输入数据在本地数据库中是否相同。这段代码工作正常,直到到达我标记为的地方,代码挂起或停止在这里。一旦代码达到条件,它就可以完美地运行,并将notifier赋值为true,但它没有从该函数中出来,因此代码的其余部分不会执行。谁能给我解释一下原因吗?我正在构建一个Ionic AngularJS应用程序 function checklocalDB(localdb, result) { var d= $q.defer(); var identifier = fa
function checklocalDB(localdb, result) {
var d= $q.defer();
var identifier = false;
var notifier = false;
// var f = function(localdb, result){
if(localdb === false) {
console.log("inside localdb false")
var insert_into_table = "INSERT INTO preferences(description, value) VALUES ('username','" + result[0].username + "'), ('token','" + result[0].token.toString() + "')";
$cordovaSQLite.execute(db, insert_into_table).then(function (res) {
console.log("executedd")
var updateDB = "UPDATE preferences SET value='true' WHERE description='registered'";
$cordovaSQLite.execute(db, updateDB).then(function (res) {
console.log("executed")
identifier = true;
notifier = true;
//d.resolve(identifier)
var query = "SELECT id, description, value FROM preferences";
$cordovaSQLite.execute(db, query).then(function (res) {
}, function (err) {
console.error(err);
});
}, function (err) {
console.error(err);
});
});
}
else {
console.log("inside localdb true")
var dbNew = null;
var query = "SELECT id, description, value FROM preferences";
console.log(localdb)
$cordovaSQLite.execute(db, query).then(function (res) {
console.log("hhhhhhhhhhhhhh")
console.log(res.rows.item(2).value)
console.log(result[0].username)
if(res.rows.item(2).value != result[0].username) {
console.log("username different")
$cordovaSQLite.deleteDB("loanstreet_partners.db");
dbNew = $cordovaSQLite.openDB("loanstreet_partners.db");
$cordovaSQLite.execute(dbNew, "CREATE TABLE IF NOT EXISTS preferences (id integer primary key, description string, value string)").then(function (res) {
console.log("done")
var insert_into_table = "INSERT INTO preferences (description, value) SELECT 'registered' as registered, 'false' as value UNION SELECT 'logged_in', 'false'";
$cordovaSQLite.execute(db, insert_into_table).then(function (res) {
console.log("1st")
var insert_into_table = "INSERT INTO preferences(description, value) VALUES ('username','" + result[0].username + "'), ('token','" + result[0].token.toString() + "')";
$cordovaSQLite.execute(db, insert_into_table).then(function (res) {
console.log("2nd")
identifier = true;
notifier = true;
var updateDB = "UPDATE preferences SET value='true' WHERE description='registered'";
$cordovaSQLite.execute(db, updateDB).then(function (res) {
}, function (err) {
console.error(err);
});
});
}, function (err) {
console.error(err);
});
}, function (err) {
console.error(err);
});
}
else {
notifier = true;
console.log("im here")
return notifier;
// ***code hangs or stops here***
}
}, function (err) {
console.error(err);
});
}
// ***this is never executed because it still remains false***
if(notifier === true) {
console.log(identifier)
console.log(notifier)
d.resolve(identifier)
}
return d.promise;
// watch identifier when value change then only resolve
//d.resolve(identifier)
//return d.promise;
}
感谢您的帮助您需要了解
异步
编程<当执行行if(notifier==true)
时,code>notifier将始终为false
,因为到那时,上面的异步处理程序将不会执行。@alex实际上tht不是我的错误所在,因为我已经处理了tht。d error是我在此处放置d reside代码挂起或停止的地方,放置行console.log('notifier:',notifier)代码>紧靠您的***下方,这永远不会执行,因为它仍然是错误的***
注释。我相信您会在控制台中看到它(或预先抛出的错误)。您需要研究异步
编程<当执行行if(notifier==true)
时,code>notifier
将始终为false
,因为到那时,上面的异步处理程序将不会执行。@alex实际上tht不是我的错误所在,因为我已经处理了tht。d error是我在此处放置d reside代码挂起或停止的地方,放置行console.log('notifier:',notifier)代码>紧靠您的***下方,这永远不会执行,因为它仍然是错误的***
注释。我相信您会在控制台中看到它(或预先抛出的错误)。您需要研究异步
编程<当执行行if(notifier==true)
时,code>notifier
将始终为false
,因为到那时,上面的异步处理程序将不会执行。@alex实际上tht不是我的错误所在,因为我已经处理了tht。d error是我在此处放置d reside代码挂起或停止的地方,放置行console.log('notifier:',notifier)代码>紧靠您的***下方,这永远不会执行,因为它仍然是错误的***
注释。我相信您会在控制台中看到它(或预先抛出的错误)。