2014-12-02 69 views
0

我使用離子和angularjs的憑據驗證工作,我想要的是當用戶已經登錄然後去成員頁面,當有請求http後/ get/else我想添加http頭部請求,我是成功的,如果不使用SQLite,但當我改變我的代碼使用SQLite(ngCordova插件)我得到錯誤。它在我的Xcode日誌中沒有任何信息返回空白。 這裏的代碼sqlite ngCordova使用離子返回空白

//app.js

'use strict'; 

var db = null; 
angular.module('logiseraApp', ['ionic','ngCordova']) 
.run(function($ionicPlatform, $cordovaSQLite) { 

    $ionicPlatform.ready(function() { 
    //SQLite database 
    db = $cordovaSQLite.openDB("my.db", 1); 
    //create tables 
    db.transaction(function(tx) { 
     //create session table 
     tx.executeSql('CREATE TABLE IF NOT EXISTS session (id integer primary key, name varchar(50), value text)'); 

     }, function(e) { 
     console.log("ERROR: " + e.message); 
    }); 
    }); 
}) 

//this is my focus error 
//i check if session is not empty 
//and fetch user data then store to config.headers 
//there is no error if i"m not using sqlite 
.factory('authInterceptor', function($location, $q, $cordovaSQLite) { 
    return { 
    request: function(config) { 
     config.headers = config.headers || {}; 

     var query = "SELECT value from session where name = ? limit 1"; 
     $cordovaSQLite.execute(db, query, ["userdata"]).then(function(res) { 
      if(res.rows.length > 0){ 
       console.log("result length", res.rows.length); 
       var userdata = JSON.parse(res.rows.item(0).value); 
       //apache 
       //config.headers.token_user_id = userdata.user_id; 
       //nginx 
       config.headers['Token-User-Id'] = userdata.user_id; 
      } 
     }, function (err) { 
      console.error(err); 
     }); 

     return config; 
    } 
    }; 
}) 

.config(function($stateProvider, $urlRouterProvider, $httpProvider) { 
    $httpProvider.interceptors.push('authInterceptor'); 
}); 

謝謝

回答

0

嘗試用不同的sqlite的語句,其中整數,關鍵,VARCHAR(50)和文本類型有不同的嘗試比如刪除:

tx.executeSql('CREATE TABLE IF NOT EXISTS session (id, name, value)'); 

整數和文本類型應該是一個問題,但VARCHAR不是東東在使用文本類型時使用。這應該至少創建一個表,如果它還不存在。

我工作的一個科爾多瓦的項目,我們使用SQLite太多,但在我們的sqlite的語句僅獨特被用於代理鍵。我們使用這個plugin,所有的數據,如浮點數或字符串,都根據它們的原始類型進行保存。