nach all meinen Problemen mit SQLite, wollte ich eine andere Lösung für meine Ionic App verwenden. LokiJS wurde oft erwähnt, also habe ich es versucht. Aber auch mit LokiJS habe ich kein Glück.Ionic + LokiJS produziert immer "ionic.bundle.js: 26794 TypeError: Kann Eigenschaft nicht 'einfügen' von undefined"
Im Moment habe ich einen sehr einfachen Code, der ohne Probleme funktionieren sollte:
.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()
Aber ich bekomme immer die Meldung:
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
Bitte helfen. Vielleicht habe ich das gleiche Problem mit SQLite und LokiJS? Aber ich kann das Problem nicht finden ...
Danke, Christian.
UPDATE - 2016-08-05
Also, ich wieder mit dem Suchen Stunden Stunden verbracht und versucht, und immer mit den gleichen Fehler zu enden. Ich frage mich, warum alle Tutorials gleich aussehen und zu funktionieren scheinen, aber für mich ist es unmöglich, es zum Laufen zu bringen.
Ich schrieb vollständig meine Fabrik:
(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
ich die PersistenceService.initDB() im Lauf() aufrufen meiner app.js. DAS FUNKTIONIERT!
Alle meine Seiten (4) haben ihren eigenen Controller, über den Controller als verwendet. In der Steuerung meiner ersten Seite versuchte ich diesen Code:
// 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 funktioniert! Ich kann die korrekte Information in der Console sehen, und ich bekomme keinen Fehler.
Das wird nächste Code direkt nach dem Code oben in dem gleichen Controller platziert:
// Test #2: Add a project
PersistenceService.addProject({ name : 'odin' });
Und es produziert die folgenden Zeilen (und erros) auf der Konsole
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]
, die ein wenig ist seltsam, denn für mich sieht es so aus, dass addProject() bevore getAllProjects() heißt?
Also habe ich die Zeile console.log (lkrDb); in der addProject-Funktion, um zu sehen, ob die Datenbank geladen ist, und ich habe weitere Tests durchgeführt. Es stellte sich heraus, dass die initDB in der app.js funktioniert, und dass die addProject-Funktion auch mit der geöffneten Datenbank arbeiten kann.
Meine Schlussfolgerung ist, dass ich etwas falsch in der Steuerung oder mit der getAllProjects-Funktion !?
ich weitersuchen werde, aber ich würde wirklich für jeden Hinweis dankbar sein ...
Danke, Christian.
Welche Ausgabe haben Sie von dieser Linie 'console.log (projects1);' – sawbeanraz