簡體   English   中英

帶有離子,交易錯誤的登錄頁面

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM