0

我使用 ionic 和 angularjs 进行凭据验证,我想要的是当用户已经登录然后转到成员页面,当有 http post/get/else 请求时,我想添加 http 标头请求,如果不使用我是成功的sqlite 但是当我使用 sqlite(ngCordova 插件)更改我的代码时出现错误。它返回空白,我的 Xcode 日志中没有任何信息。这里的代码

//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');
});

谢谢,

4

1 回答 1

0

尝试使用不同的 sqlite 语句进行不同的尝试,其中删除整数、键、varchar(50) 和文本类型,例如:

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

integer 和 text 类型应该不是问题,但使用 text 类型时不需要 varchar。如果它尚不存在,这至少应该创建一个表。

我正在做一个科尔多瓦项目,我们也使用 sqlite,但在我们的 sqlite-statements 中,只有unique用于代理键。我们使用此插件,所有数据(如浮点数或字符串)都根据其原始类型保存。

于 2015-02-24T17:45:26.317 回答