2016-08-03 8 views
0

에 문제가 발생했습니다. 이오닉 앱에 다른 해결책을 사용하고 싶습니다. LokiJS가 종종 언급되었으므로 시도했습니다. 그러나 LokiJS와 나는 운이 없다. 순간 Ionic + LokiJS는 항상 "ionic.bundle.js : 26794를 생성합니다. TypeError : '정의되지 않은'insert ''undefined '를 읽을 수 없습니다. 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와 같은 문제를 가지고 있을까? 하지만 문제를 찾을 수 없습니다 ...

감사합니다, 기독교.


UPDATE - 그래서, 내가 노력하고, 항상 같은 오류가 끝나는 검색 다시 시간의 시간을 보냈다 2016년 8월 5일

. 나는 왜 모든 튜토리얼이 똑같아 보이고 작동하는지 궁금해하지만 나에게 그것을 실행시키는 것은 불가능하다. 내 app.js.의 내가 실행에 PersistenceService.initDB() (전화

(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 

) :

나는 완전히 내 공장을 다시 썼다 이것은 작동합니다!

내 모든 페이지 (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); 
    } 
    }); 

IT WORKS! 나는 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-function에서 데이터베이스가로드되었는지 확인하고 더 많은 테스트를 수행했습니다. app.js의 initDB가 작동하고 addProject-function이 열린 데이터베이스에서도 작동한다는 것이 밝혀졌습니다.

내 결론은 내가 컨트롤러에서 잘못되었거나 getAllProjects-function!

내가 검색을 계속 하겠지만, 내가 어떤 힌트 정말 thankfull이 될 것입니다 ...

감사합니다, 기독교.

+0

이 라인에서 당신을 어떤 출력 '을 console.log (1 프로젝트)' – sawbeanraz

답변

0

이제 나는 그 약속을 이해합니다! 이것은 많은 도움이되었습니다. http://andyshora.com/promises-angularjs-explained-as-cartoon.html

그러나 이것이 실제로 처리해야하는 항목이더라도 문제는 아닙니다. 문제는 내가 사용한 튜토리얼에 기반한다. 이유와 방법은 모르겠지만 tutorilas가 작동하며 내 앱은 단순히 getAllProjects() 함수를 호출하지 않는 한 projects1 변수에 데이터가 없습니다. autoloadCallback 함수를 자동으로 호출하지 않습니다!? LokiJS 프로젝트에서 내가 뭘 잘못했는지 또는 autoloadCallback에 대한 아이디어가 아닌지 질문합니다.

나는 완벽한 작업 솔루션을 때, 내가 여기에 게시됩니다 ...

0

1 프로젝트가 제대로 초기화되지 않았습니다 만이 작동합니다 db.loadDatabase()callback

projects1.insert()를 호출해야합니다. 이것은 단지 스파이크 일 뿐이므로 데이터베이스 작업에 factory을 사용해보십시오. 그리고 자바 스크립트 변수 범위를 거쳐 다시이 문제에 직면하지 마십시오.

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