登录页面包含离子,交易错误

时间:2016-07-28 08:34:25

标签: javascript ionic-framework hybrid

我有一个问题,我只是一个初学者。我想用离子做一个登录页面,但是我收到的错误是:

  

ReferenceError:res未定义
  在Object.loginUser(http://localhost:8100/js/services.js:41:25
  在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)
  在http://localhost:8100/lib/ionic/js/ionic.bundle.js:65427:9
  在范围。$ eval(http://localhost:8100/lib/ionic/js/ionic.bundle.js:30395:28
  在范围。$ 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

     

错误trans [object 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;
        }
    }
});

如果您帮助我,或者您对如何验证此登录有任何建议,我将不胜感激

**

第1版

**

我已经改变了方法,所以现在我的控制器变成了:

.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模拟器时,我从事务中得到了一个错误

警告(&#34;错误转换&#34; + e);它说错误tran [对象对象]

我也提醒警报(db),它说:[object object],

1 个答案:

答案 0 :(得分:0)

res仅存在于该行开头的函数中:

tx.executeSql("SELECT username, password FROM users where username = ?", [username], function(tx, res) {

......这是该职能的一个论据。

你试图在该功能之后使用它几行。它只能在里面使用。

相关阅读:How do I return the response from an asynchronous call?