2016-08-03 18 views
0

,我想为我的Ionic应用程序使用不同的解决方案。 LokiJS经常被提及,所以我尝试了它。但也与LokiJS我没有运气。Ionic + LokiJS总是生成“ionic.bundle.js:26794 TypeError:在SQLite遇到所有问题后,无法读取未定义的属性”插入“

目前,我有一个非常基本的代码,这应该没有问题的工作:

.controller('ProjectsController', ['$scope', '$state', '$ionicPlatform', function($scope, $state, $ionicPlatform) { 
    var pcVm = this; 

    var db; 
    var projects1; 

    $ionicPlatform.ready(function(){ 

    var idbAdapter = new LokiIndexedAdapter('loki'); 
    db = new loki('lkr-v4', { 
     autoload: true, 
     autoloadCallback : getAllProjects, 
     autosave: true, 
     autosaveInterval: 10000, 
     adapter: idbAdapter 
    }); 

    function getAllProjects() { 
     var options = {}; 
     db.loadDatabase(options, function() { 
     projects1 = db.getCollection('projects'); // GET 
     if (!projects1) { 
      projects1 = db.addCollection('projects'); // ADD 
     } 
     console.log('Databaseinformation'); 
     console.log(db); 
     console.log('Collectioninformation'); 
     console.log(projects1); 
     }); 
    } 

    console.log('Insert something'); 
    projects1.insert({ name : 'odin' }); 
    }); // End of .ready() 

但我总是得到消息:

ionic.bundle.js:26794 TypeError: Cannot read property 'insert' of undefined 
    at controllers.js:309 
    at ionic.bundle.js:56230 
    at Object.ready (ionic.bundle.js:2140) 
    at Object.ready (ionic.bundle.js:56223) 
    at new <anonymous> (controllers.js:283) 
    at Object.instantiate (ionic.bundle.js:18010) 
    at $controller (ionic.bundle.js:23412) 
    at self.appendViewElement (ionic.bundle.js:59900) 
    at Object.render (ionic.bundle.js:57893) 
    at Object.init (ionic.bundle.js:57813)(anonymous function) @ ionic.bundle.js:26794(anonymous function) @ ionic.bundle.js:23507$broadcast @ ionic.bundle.js:30720$state.transition.resolved.then.$state.transition @ ionic.bundle.js:52157processQueue @ ionic.bundle.js:29127(anonymous function) @ ionic.bundle.js:29143$eval @ ionic.bundle.js:30395$digest @ ionic.bundle.js:30211$apply @ ionic.bundle.js:30503done @ ionic.bundle.js:24824completeRequest @ ionic.bundle.js:25022requestLoaded @ ionic.bundle.js:24963 
controllers.js:301 Databaseinformation 

请帮助。也许我有与SQLite和LokiJS相同的问题?但我找不到问题...

谢谢, 基督徒。


更新 - 2016年8月5日

于是,我又花了几个小时小时的搜索,尝试,始终与同样的错误结束了。我想知道为什么所有的教程看起来都是一样的,而且看起来可行,但对我来说,它不可能运行。

我完全重写我的工厂:我app.js.的

(function() { 
    'use strict'; 

    angular.module('lkr-v4') 

    .factory('PersistenceService', ['$q', 'Loki', PersistenceService]); 

     function PersistenceService($q, Loki) { 
     // Needed variables 
     var lkrDb; // Database 
     var projects; // Collection of JSON strings 

     // Accessible Members Up Top 
     // https://github.com/johnpapa/angular-styleguide/tree/master/a1#style-y052 
     var pService = { 
      initDB: initDB, 
      getAllProjects: getAllProjects, 
      addProject: addProject, 
      updateProject: updateProject, 
      deleteProject: deleteProject 
     }; 
     return pService; 

     // Initialization of the database 
     function initDB() { 
      var idbAdapter = new LokiIndexedAdapter('loki'); 
      lkrDb = new loki('lkr-v4', { 
      autoload: true, 
      autoloadCallback: getAllProjects, 
      autosave: true, 
      autosaveInterval: 10000, 
      adapter: idbAdapter 
      }); 
     } 

     // Function to return a collection of JSON strings (all found projects) in the LokiJS database file 
     function getAllProjects() { 
      // What is $q? See https://docs.angularjs.org/api/ng/service/$q 
      return $q(function (resolve, reject) { 
      var options = {}; 
      lkrDb.loadDatabase(options, function() { 
       projects = lkrDb.getCollection('projects'); // GET! 
       // If the database file is empty 
       if (!projects) { 
       projects = lkrDb.addCollection('projects'); // ADD! 
       } 
       resolve(projects.data); 
      }); 
      }); 
     } 

     // Function to add a project 
     function addProject(project) { 
      if(lkrDebug) { 
      console.log('Inside of addProject from the factory'); 
      console.log('This is the projects object'); 
      console.log(this.projects); 
      console.log('This is the given value of the new project'); 
      console.log(project); 
      } 
      projects.insert(project); 
     } 

     // Function to update a project 
     function updateProject(project) { 
      projects.update(project); 
     } 

     // Function to delete a project 
     function deleteProject(project) { 
      projects.remove(project); 
     } 

     } // End of function PersistenceService 

})(); // End of IIFE 

我所说的PersistenceService.initDB()在run()这工作!

我的所有页面(4)都有自己的控制器,用于控制器上。在我的第一页的控制器中,我试过这个代码:

// Test #1: Get data 
    PersistenceService.getAllProjects().then(function(projects) { 
    pcVm.projects = projects; 
    if(lkrDebug) { 
     console.log('Inside of getAllProjects().then() from the controller'); 
     console.log('This is the project object from the PersistenceService'); 
     console.log(projects); 
     console.log('And this ist the pcVm.projects object from the controller'); 
     console.log(pcVm.projects); 
    } 
    }); 

它工作!我可以看到consloe中的正确信息,并且我没有发现任何错误。

下一个代码是直接的代码后在同一个控制器上面放置:

// Test #2: Add a project 
    PersistenceService.addProject({ name : 'odin' }); 

并可以产生控制台

Inside of addProject from the factory 
persistence.services.js:65 This is the projects object 
persistence.services.js:66 undefined 
persistence.services.js:67 This is the given value of the new project 
persistence.services.js:68 Object {name: "odin"} 
ionic.bundle.js:26794 TypeError: Cannot read property 'insert' of undefined 
    at Object.addProject (persistence.services.js:70) 
    at new ProjectsController (projects.controller.js:31) 
    at Object.instantiate (ionic.bundle.js:18010) 
    at $controller (ionic.bundle.js:23412) 
    at self.appendViewElement (ionic.bundle.js:59900) 
    at Object.render (ionic.bundle.js:57893) 
    at Object.init (ionic.bundle.js:57813) 
    at self.render (ionic.bundle.js:59759) 
    at self.register (ionic.bundle.js:59717) 
    at updateView (ionic.bundle.js:65398)(anonymous function) @ ionic.bundle.js:26794(anonymous function) @ ionic.bundle.js:23507$broadcast @ ionic.bundle.js:30720$state.transition.resolved.then.$state.transition @ ionic.bundle.js:52157processQueue @ ionic.bundle.js:29127(anonymous function) @ ionic.bundle.js:29143$eval @ ionic.bundle.js:30395$digest @ ionic.bundle.js:30211$apply @ ionic.bundle.js:30503done @ ionic.bundle.js:24824completeRequest @ ionic.bundle.js:25022requestLoaded @ ionic.bundle.js:24963 
projects.controller.js:22 Inside of getAllProjects().then() from the controller 
projects.controller.js:23 This is the project object from the PersistenceService 
projects.controller.js:24 [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object] 
projects.controller.js:25 And this ist the pcVm.projects object from the controller 
projects.controller.js:26 [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object] 

这一点上下面的行(和误差修改)奇怪,因为对我来说,它看起来像addProject()被称为bevore getAllProjects()?

所以我添加了行console.log(lkrDb);在addProject函数中,查看数据库是否被加载,并且我做了更多的测试。事实证明,app.js中的initDB正在工作,并且addProject函数也可以用于打开的数据库。

我的结论是,我在控制器或getAllProjects函数中做错了什么!?

我会继续搜索,但我对任何暗示真的心存感激......

感谢, 基督徒。

+0

什么输出你从这一行'的console.log(projects1);' – sawbeanraz

回答

0

现在我明白了承诺的事!这对我有很大的帮助:http://andyshora.com/promises-angularjs-explained-as-cartoon.html

但是,即使这是我必须真正照顾的东西,这不是问题。问题基于我使用的教程。我不知道为什么以及如何,但tutorilas的工作,我的应用程序并不仅仅是因为:只要我不调用getAllProjects()函数,projects1变量没有数据。 autoloadCallback不会自动调用该函数!?我在LokiJS项目中打开了一个问题,询问我做错了什么,或者如果这不是autoloadCallback的想法。

当我有一个完整的工作方案,我将它张贴在这里...

0

projects1不正确初始化你只有后db.loadDatabase()callback

这应该叫projects1.insert()。这只是一个高峰,尝试使用factory进行数据库操作。并通过javascript变量范围,以便您不再面临这个问题。

db.loadDatabase(options, function() { 
    projects1 = db.getCollection('projects'); 
    if (!projects1) { 
    projects1 = db.addCollection('projects'); 
    } 
    projects1.insert({name: 'odin'});  

    }); 

希望它能帮助,快乐编码:)

更新:请更新您的实际问题的问题。

+0

嗨Rathan!谢谢,你的例子似乎工作。我想通过本教程http://gonehybrid.com/how-to-use-lokijs-for-local-storage-in-your-ionic-app这样的工厂来实现,但它从未奏效。聊天中的某个人告诉我要先以最基本的方式尝试它,然后通过反复试验找出问题。现在,它的工作原理,我想我明白了为什么,但我不知道它为什么在教程中起作用,但不是用我的代码... – cjs1976

+0

你得到什么错误?如果你在这里提到日志,那么我可以帮你调试。 –

+0

我又花了几个小时。我现在尝试按照John Papa(Angular 1 Style Guide)的建议改变我的应用程序的风格,并且我发现了很多错误,但我认为它们与我的问题没有关系。但是因为这个原因,我也重写了我的PersistenceService工厂。但我最终会遇到同样的问题。初始化工作,getAll工作,但插入/更新/删除他们不。 – cjs1976

相关问题