0

I'm trying to use NeDB database for persistence of application data. I'm trying to use the following approach to connect to my database like the following:

var Datastore = require('nedb')
  , path = require('path')
  , db = new Datastore({ filename: path.join(require('nw.gui').App.dataPath, 'something.db') }

But unfortunatly it fails because this works only client code withit <script></script> tags in html file. How could I do same thing on server side?

Erik
  • 14,060
  • 49
  • 132
  • 218
  • node-webkit is something to work on desktop. If you want server-side DB, you would use some AJAX calls to your server, I guess. And the NEDB query logic would be on server, too. – Timothy Ha Nov 28 '14 at 09:19
  • @Timothy No I need to use database on local machine. Not server side – Erik Nov 28 '14 at 09:36
  • please clarify, what is your problem then? From index.html you could include something like db.js, and initialize db connection on window.onload. Queries to DB can be done on other events, like clicking a link in HTML. Would that work for you? – Timothy Ha Nov 28 '14 at 13:25
  • I need to initialize db on "server side" in `node-main` script – Erik Nov 28 '14 at 15:47
  • I see that you rewrote your question in another topic and already got your answer :) – Timothy Ha Nov 28 '14 at 19:47
  • No. I'm still unable to get work `require('nw.gui')` in nodejs context. But now I can see the following error: http://stackoverflow.com/questions/24392756/node-module-nw-gui-not-found#answer-26321866 – Erik Nov 28 '14 at 22:27
  • Could you elaborate a little more on the structure of your app? Maybe you can put it to github? In my own case it's all working, though I am not using nedb, but sqlite3 module. – Timothy Ha Nov 29 '14 at 16:17

1 Answers1

1

The problem here is, that you cannot require('nw.gui') in the nodejs context, because in the nodejs environment is no window. No window, no gui. So what you can do, is just create a script tag in your main file (index.html) with the src to your db.js file with the above content, it should work fine.

in index.html

<script src="db/db.js"></script>

in db/db.js

var Datastore = require('nedb')
,   path = require('path')
,   dbFile = path.join(require('nw.gui').App.dataPath, 'something.db')
,   db = new Datastore({ filename: dbFile })
webdeb
  • 12,993
  • 5
  • 28
  • 44
  • Hey @Erik, I guess you already came across this problem ;) Anyway, this link should also help: https://github.com/nwjs/nw.js/issues/2280#issuecomment-54149072 – webdeb Aug 19 '15 at 15:54