[英]Login page with ionic, transaction errors
我有一个问题,我只是一个初学者。 我想用ionic做一个登录页面,但是出现一条错误消息:
ReferenceError:未定义res
在Object.loginUser( http:// localhost:8100 / js / services.js:41:25 )
在Scope。$ scope.doLogin( http:// localhost:8100 / js / controllers.js:23:22 )
在fn(在编译时评估( http:// localhost:8100 / lib / ionic / js / ionic.bundle.js:27638:15 ) ,: 4:212)
在http:// localhost:8100 / lib / ionic / js / ionic.bundle.js:65427:9
在Scope。$ eval( http:// localhost:8100 / lib / ionic / js / ionic.bundle.js:30395:28 )
在Scope。$ apply( http:// localhost:8100 / lib / ionic / js / ionic.bundle.js:30495:25 )
在HTMLAnchorElement。 ( http:// localhost:8100 / lib / ionic / js / ionic.bundle.js:65426:13 )
在defaultHandlerWrapper( http:// localhost:8100 / lib / ionic / js / ionic.bundle.js:16787:11 )
在HTMLAnchorElement.eventHandler( http:// localhost:8100 / lib / ionic / js / ionic.bundle.js:16775:9 )
在triggerMouseEvent( http:// localhost:8100 / lib / ionic / js / ionic.bundle.js:2953:7 )错误转换[对象SQLTransaction]
我的controllers.js:
.controller('loginCtrl', function($scope, LoginService, $ionicPopup, $state) {
$scope.data = {};
$scope.doLogin = function() {
username = $scope.data.user;
password = $scope.data.pwd;
alert(username);
LoginService.loginUser($scope.data.user, $scope.data.pwd).success(function(data) {
$state.go('tableauDeBord');
}).error(function(data) {
var alertPopup = $ionicPopup.alert({
title: 'Login failed!',
template: 'Please check your credentials!'
});
});
}
})
我的services.js
.service('LoginService', function($q) {
return {
loginUser: function(name, pw) {
var deferred = $q.defer();
var promise = deferred.promise;
var self = this;
db.transaction(function(tx) {
// running a SQL query
alert(username);
alert(password);
tx.executeSql("SELECT username, password FROM users where username = ?", [username], function(tx, res) {
var len = res.rows.length;
// for (var i = 0; i < len; i++) { // loop as many times as there are row results
alert(res.rows.item(0).username + ' : ' + res.rows.item(0).password);
// //alert( res.rows.item(i).username +' : '+ res.rows.item(i).password ); // showing the results
// }
if (username == res.rows.item(0).username && password == res.rows.item(0).password) {
deferred.resolve('Bienvenue ' + username + '!');
alert("seccess");
} else {
deferred.reject('Informations erronées.');
}
}, function(e) {
console.log("error trans " + e);
alert("ERROR SQL: " + e.message);
});
});
if (name == res.rows.item(0).username && pw == res.rows.item(0).password) {
deferred.resolve('Welcome ' + name + '!');
} else {
deferred.reject('Wrong credentials.');
}
promise.success = function(fn) {
promise.then(fn);
return promise;
}
promise.error = function(fn) {
promise.then(null, fn);
return promise;
}
return promise;
}
}
});
如果您能帮助我,或者您对如何验证此登录名有任何建议,我将不胜感激
**
**
我已经更改了方法,所以现在我的控制器变为:
.controller('loginCtrl', function($scope, LoginService, $ionicPopup, $state) {
$scope.data = {};
$scope.doLogin = function() {
username = $scope.data.user;
password = $scope.data.pwd;
var self = this;
db.transaction(function(tx) {
// running a SQL query
tx.executeSql("SELECT username, password FROM users where username = ?", [$scope.data.user], function(tx, res) {
var len = res.rows.length;
// for (var i = 0; i < len; i++) { // loop as many times as there are row results
alert(username);
alert( res.rows.item(0).username +' : '+ res.rows.item(0).password );
// //alert( res.rows.item(i).username +' : '+ res.rows.item(i).password ); // showing the results
// }
if (username == res.rows.item(0).username && password == res.rows.item(0).password) {
deferred.resolve('Bienvenue ' + username + '!');
alert("seccess");
$state.go('tableauDeBord');
} else {
var alertPopup = $ionicPopup.alert({
title: 'Login failed!',
template: 'Please check your login informations!'
});
}
}, function(e) {
alert("error trans " + e);
var alertPopup = $ionicPopup.alert({
title: 'Login failed!',
template: 'Please check your sql trans! '
});
});
});
}
})
app.js
var username = null;
var db = null;
angular.module('app', ['ionic', 'app.controllers', 'app.routes', 'app.services', 'app.directives'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
/* DATABASE INTEGRATION */
db = window.openDatabase("utilisateurs.db", "1.0", "utilisateurs.db", 200000);
//alert("sqlitePlugin not loaded");
});
})
当我启动android模拟器时,我从交易中得到一个错误
alert(“ error trans” + e); 并说错误tran [object object]
我也对Alert(db)发出了警报,它说:[object object],
res
仅存在于从以下行开始的函数中:
tx.executeSql("SELECT username, password FROM users where username = ?", [username], function(tx, res) {
…这是该函数的参数之一。
您正在尝试在该功能后几行使用它。 它只能在内部使用。
相关阅读: 如何返回异步调用的响应?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.