[英]Login page with ionic, transaction errors
I have an issue, I am just a beginner with this. 我有一个问题,我只是一个初学者。 I would like to do a login page with ionic, but I get an error that says : 我想用ionic做一个登录页面,但是出现一条错误消息:
ReferenceError: res is not defined ReferenceError:未定义res
at Object.loginUser ( http://localhost:8100/js/services.js:41:25 ) 在Object.loginUser( http:// localhost:8100 / js / services.js:41:25 )
at Scope.$scope.doLogin ( http://localhost:8100/js/controllers.js:23:22 ) 在Scope。$ scope.doLogin( http:// localhost:8100 / js / controllers.js:23:22 )
at fn (eval at compile ( http://localhost:8100/lib/ionic/js/ionic.bundle.js:27638:15 ), :4:212) 在fn(在编译时评估( http:// localhost:8100 / lib / ionic / js / ionic.bundle.js:27638:15 ) ,: 4:212)
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:65427:9 在http:// localhost:8100 / lib / ionic / js / ionic.bundle.js:65427:9
at Scope.$eval ( http://localhost:8100/lib/ionic/js/ionic.bundle.js:30395:28 ) 在Scope。$ eval( http:// localhost:8100 / lib / ionic / js / ionic.bundle.js:30395:28 )
at Scope.$apply ( http://localhost:8100/lib/ionic/js/ionic.bundle.js:30495:25 ) 在Scope。$ apply( http:// localhost:8100 / lib / ionic / js / ionic.bundle.js:30495:25 )
at HTMLAnchorElement. 在HTMLAnchorElement。 ( http://localhost:8100/lib/ionic/js/ionic.bundle.js:65426:13 ) ( http:// localhost:8100 / lib / ionic / js / ionic.bundle.js:65426:13 )
at defaultHandlerWrapper ( http://localhost:8100/lib/ionic/js/ionic.bundle.js:16787:11 ) 在defaultHandlerWrapper( http:// localhost:8100 / lib / ionic / js / ionic.bundle.js:16787:11 )
at HTMLAnchorElement.eventHandler ( http://localhost:8100/lib/ionic/js/ionic.bundle.js:16775:9 ) 在HTMLAnchorElement.eventHandler( http:// localhost:8100 / lib / ionic / js / ionic.bundle.js:16775:9 )
at triggerMouseEvent ( http://localhost:8100/lib/ionic/js/ionic.bundle.js:2953:7 ) 在triggerMouseEvent( http:// localhost:8100 / lib / ionic / js / ionic.bundle.js:2953:7 )error trans [object SQLTransaction] 错误转换[对象SQLTransaction]
My controllers.js : 我的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!'
});
});
}
})
My services.js 我的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;
}
}
});
i would be grateful if u help me, or if you have any suggestion about how to verify this login 如果您能帮助我,或者您对如何验证此登录名有任何建议,我将不胜感激
** **
** **
i've changed the method so now my controller became : 我已经更改了方法,所以现在我的控制器变为:
.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 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");
});
})
and when i start the android emulator i got an error from the transaction 当我启动android模拟器时,我从交易中得到一个错误
alert("error trans " + e); alert(“ error trans” + e); and it says error tran [object object] 并说错误tran [object object]
also i've made an alert to alert(db) and it says : [object object], 我也对Alert(db)发出了警报,它说:[object object],
res
only exists in the function that starts on the line: res
仅存在于从以下行开始的函数中:
tx.executeSql("SELECT username, password FROM users where username = ?", [username], function(tx, res) {
… it is one of the arguments to that function. …这是该函数的参数之一。
You are trying to use it several lines after that function. 您正在尝试在该功能后几行使用它。 It can only be used inside it. 它只能在内部使用。
Related reading: How do I return the response from an asynchronous call? 相关阅读: 如何返回异步调用的响应?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.