2

I have trying to create a database with a collection added to this database and the changes saved to a IndexedDB.

Below is my code

  1. Two controllers SaveController and LoadController.

myApp.controller('SaveController', ['$scope', 'Loki', function ($scope, Loki) {

// SAVE : will save App/Key/Val as 'finance'/'test'/{serializedDb}
// if appContect ('finance' in this example) is omitted, 'loki' will be used
var idbAdapter = new LokiIndexedAdapter('finance');

var db = new loki('ProjectDb', { adapter: idbAdapter });

var coll = db.addCollection('SampleCollection');
coll.insert({ SampleId: 'Sample text.....' });

db.saveDatabase();  // could pass callback if needed for async complete

}]);

and then in my LoadController i use the

myApp.controller('LoadController', ['$scope', 'Loki', function ($scope, Loki) {

var idbAdapter = new LokiIndexedAdapter('finance');

var db = new loki('ProjectDb', { adapter: idbAdapter, autoload: true });
db.loadDatabase({}, function (result) {
    console.log(result);
});

alert(db.getCollection("SampleCollection"));

}]);

I get a null when i alert "alert(db.getCollection("SampleCollection"));" . It never enters the call back of the "loadDatabase" method.

Is there something that i am missing ?

IndexedDB in Browser

IndexedDB

Here the page html

Page HTML

lokijs script controller

Edit for default localstorage implementation

I use the default implementation for loki js and i try to load the offline db is shows result as null every time even though the db exist

var offlineDb = new loki('DbOfflineNew');
    offlineDb.loadDatabase({},function (result) {
        console.log(result);
        if (result == null) {
            alert('loading for first time..');
        }
        else {
            alert('existing load..');
        }
    });

Every time the alert "loading for first time.. " is fired.. Any thing i am missing here..?

user581157
  • 1,327
  • 4
  • 26
  • 64

1 Answers1

5

Basically all your logic needs to be in the loadDatabase callback. If you try to console.log the collection before it's loaded it will be null. Many people fall in this trap.

In other words:

myApp.controller('LoadController', ['$scope', 'Loki', function ($scope, Loki) {

var idbAdapter = new LokiIndexedAdapter('finance');

var db = new loki('ProjectDb', { adapter: idbAdapter, autoload: true });
db.loadDatabase({}, function (result) {
    console.log(result);
    // put your log call here.
    alert(db.getCollection("SampleCollection"));
});
}]);

Hope this helps.

Joe Minichino
  • 2,793
  • 20
  • 20
  • This would be the canonical answer ;) – Darragh Enright Aug 06 '15 at 16:38
  • Hi Joe, I have edited my question this time to load database for default loki js implementation and everytime the value in the callback for loadDatabase method comes with a null value ? – user581157 Oct 26 '15 at 13:46
  • Got it to working checked once again on the http://lokijs.org/#/docs#load site found that the result is never returned for default local storage implementation . Thanks – user581157 Oct 27 '15 at 11:17