【问题标题】:sqlite ngCordova using ionic return blanksqlite ngCordova 使用离子返回空白
【发布时间】:2015-02-25 02:18:58
【问题描述】:

我使用 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');
});

谢谢,

【问题讨论】:

    标签: angularjs sqlite cordova-3 ionic


    【解决方案1】:

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

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

    整数和文本类型应该成为问题,但使用文本类型时不需要 varchar。如果它尚不存在,这至少应该创建一个表。

    我正在做一个 cordova 项目,我们也使用 sqlite,但在我们的 sqlite 语句中,只有 unique 用于 代理键。我们使用这个plugin,所有的数据,比如float's或string's都根据它们的原始类型保存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-20
      相关资源
      最近更新 更多