0

I am trying to use my existing DB in LokiJS but my autoloadCallback does not fire.

Am using console.log('from add collection don') to know when it fires but it never does and when I try to add data to the DB it fails. It only works when I set my collection variable locally:

  var user = null
      db = new loki("myuser.json",{adapter: adapter}, {
        autosave: true,
        autosaveInterval: 5000,
        autoload: true,
        autoloadCallback: function(){
            db_ready = true;
            console.log('from add collection don')
            if(db.getCollection("myaccount") == null ){
                myusers = db.addCollection("myaccount");
            }
       
        }
    });

function py_userlogin(username,password,islogin){
        myusers.insert({
            username:username,
            password:password,
            islogin:islogin
        },function(err,don){
            console.log( JSON.stringify(err) + JSON.stringify(don))
        });
        console.log(myusers.data);
        db.saveDatabase();
    }
Kamafeather
  • 8,663
  • 14
  • 69
  • 99
Lawrence Nwoko
  • 79
  • 1
  • 10

1 Answers1

3

You are setting options in a third variable that is not present, it should be only:

new loki(file, [options]);

adapter is an option just like the others, like this:

var user = null
      db = new loki("myuser.json",{
        adapter: adapter,
        autosave: true,
        autosaveInterval: 5000,
        autoload: true,
        autoloadCallback: function(){
            db_ready = true;
            console.log('from add collection don')
            if(db.getCollection("myaccount") == null ){
                myusers = db.addCollection("myaccount");
            }

        }
    });

Cheers

Roberto Lonardi
  • 569
  • 2
  • 10