2

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!

Peter Dolan
  • 1,393
  • 1
  • 12
  • 26
  • 2
    Are you using angular? I don't think we have enough details to really help you on this one. But in general, If you are not using anything like angular or react, you would include the lokijs js file on your page. If you are using react, you would import the file so webpack bundles it. For angular, I'm not sure. – Michael Cook Dec 28 '17 at 02:42
  • 1
    Hey Cadmium, thanks for the help! Not using angular or react -- when you say include the lokijs file, what do you mean exactly beyond the script? If there's a resource that explains this thoroughly (as I'm guessing it's a dumb question), could you point me that way? – Peter Dolan Dec 28 '17 at 05:06
  • 1
    Absolutely not a dumb question, I think you are just a step or two from getting to the root of the issue. I’ll try to post an example when I get the chance. – Michael Cook Dec 28 '17 at 18:49
  • 1
    Sorry for the delay. Upon further inspection the problem you had was even simpler than I suspected. I posted the answer below. – Michael Cook Dec 29 '17 at 20:37

1 Answers1

1

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.

Michael Cook
  • 1,676
  • 2
  • 26
  • 47