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 :
ReferenceError: res is not defined
at Object.loginUser ( http://localhost:8100/js/services.js:41:25 )
at 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)
at 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 )
at Scope.$apply ( http://localhost:8100/lib/ionic/js/ionic.bundle.js:30495:25 )
at HTMLAnchorElement. ( http://localhost:8100/lib/ionic/js/ionic.bundle.js:65426:13 )
at 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 )
at triggerMouseEvent ( http://localhost:8100/lib/ionic/js/ionic.bundle.js:2953:7 )error trans [object SQLTransaction]
My 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
.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
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
alert("error trans " + e); and it says error tran [object object]
also i've made an alert to alert(db) and it says : [object object],
res
only exists in the function that starts on the line:
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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.