1

I'm trying implement autologin on my monaca application. To do this, I use web sql database. Upon manually logging in, I store user email and password. On subsequent startups, I call on my SERVICE - 'dbTransaction' in my Main controller and use dbTransaction.getUser(). This successfully stores email and password into a global variable called profile.

Here's the problem. When I'm alerting profile.email after resolving the promise, it will show on the monaca IDE but not on the monaca debugger on my Iphone. Nothing shows on my phone and the function to login doesn't run either. PLease and thank you for helping.

   app.service('dbTransaction', function($rootScope, $q){
       var db;

       this.Db = function(){
           return window.openDatabase('database', "1.0", "DB", 100);
       };
       this.errorCb = function(err){
           console.log("Error occured while executing SQL: "+err.code);
       };

        this.saveUser = function(email, password){
            var self = this;
            db = self.getDb();   
            db.transaction(function(tx){
               tx.executeSql('CREATE TABLE IF NOT EXISTS user_table (email unique, password)');
               tx.executeSql('INSERT INTO OR REPLACE INTO user_table (email, password) VALUES ("' + email + '", "'+ password + '")');
               alert("wrote " + email + " " + password + " " + 'INSERT INTO user_table(email, password) VALUES ("' + email +'", "' + password + '")');
            });
        };

        this.getUser = function(){
            var self = this;
            db = self.getDb();
            var deferred = $q.defer();
            setTimeout(function(){
            db.transaction(function(tx){
                tx.executeSql('CREATE TABLE IF NOT EXISTS user_table (email unique, password)');
                tx.executeSql('SELECT * FROM user_table', [], 
                function(tx,result){
                    var loginInfo = [];
                    var len = result.rows.length;
                    if(len > 0){
                        //profile is a global variable
                        profile.email = result.rows.item(len-1).email;
                        profile.password = result.rows.item(len-1).password;
                        $rootScope.$apply(function(){deferred.resolve('worked');
                        alert(profile.password); //this successfully returns password
                        });
                    }else{
                        return;
                    }
                },  function(error){
                    alert(3);
                        deferred.reject('something went wrong!');
                    });
                });
            }, 1000);
            return deferred.promise;
        };


    });

main controller

   app.controller('main', function($scope, $rootScope, $http, dbTransaction){



        ons.ready(function() {
            var promise = dbTransaction.getUser();
            if(promise){
            promise.then(function(){
                showLoading();
                var param = {
                    email:profile.email, 
                    pass: profile.password
                };
                 alert(param.email)//this successfully returns email
                $http({
                    method: 'POST',
                    url: serverUrl + 'login.php',
                    data:param
                }).success(function(data,status,headers,config){
                    alert(data);
                    if(data.result == 1 ){
                        hideLoading();
                        alert('login okay');
                        $rootScope.loggedIn = true;
                        isLoggedIn = true;
                    }else{
                        hideLoading();
                        alert(data);
                    }
                });
            }); 
            }
        });



    });
Jongware
  • 22,200
  • 8
  • 54
  • 100
jackjoesmith
  • 951
  • 4
  • 20
  • 33

1 Answers1

1

My sql statement was written incorrectly.

It's suppose to be

tx.executeSql('INSERT OR REPLACE INTO user_table 

instead of

tx.executeSql('INSERT INTO OR REPLACE INTO user_table
jackjoesmith
  • 951
  • 4
  • 20
  • 33