0

I'm using LokiJS to save in local storage (well, I'm trying) . What I want to do is a ToDo app, my controller is as follows:

.controller('Dash', function($scope) {

    var db = new loki('loki.json');
    $scope.name="";
    $scope.lname="";

    var users=db.getCollection('users');

    if (users==null) {
      $scope.message="It's null";
      var users = db.addCollection('users');
    }else{
      $scope.message="It's ready";
    }

    $scope.insert=function(namesI, lnameI){
      users.insert({
        name: namesI,
        lname:lnameI
       }); 

   }

The issue is that everytime that I test it, the message is "It's null". Although before already I have inserted data. I mean, everytime I launch the app, the database is created.

How I can persist the data?
*I'm not using any cordova plugin.

1 Answers1

0

You are not providing a loadHandler function, so you are trying to access collections before Loki is finished loading the json file. Look at this example for a clarification on how to use the autoLoadHandler.

Also bear in mind that at some stage you need to call db.saveDatabase() to persist, or else you need to provide an autoSaveInterval value when instantiating Loki.

Joe Minichino
  • 2,793
  • 20
  • 20