javascriptlokijs

How can I use LokiJs in the browser?


I suspect this is a bit of a simple question, as I'm not very experienced with the frontend.

I'm trying to use lokijs as a datastore in a browser app, but I can't get it to run. I have <script src="lib/lokijs/src/loki-angular.js"></script> in my html, and

var db = new loki('test');
var users = db.addCollection('users');
users.insert({
  name: 'joe'
});

console.log(users.data);

in my js as a trivial test. However, I'm getting the error Uncaught ReferenceError: loki is not defined. Please let me know where' I'm going wrong, and I apologize if this is something stupid!


Solution

  • You are not far off. You mention you are referencing loki-angular.js in your html, however you should be referencing lib/lokijs/build/lokijs.min.js or even reference it from a cdn if you don't want to involve npm, I know cdnjs has it.

    With that your example should run just fine.