1

I have written NodeWebKitApp in which I save user's data (like name, uid and settings) for later use. I can insert a document fine and I can see its content. The file size is about 2KB.

But, if I close the app and then re-open it, the saved data is lost and I see a file size of 0 bytes.

Debugging, I discovered that this behavior occurs when the following line is executed:

db = new Datastore({ filename: myPath, autoload: true });

I tried to debug nedb/persistance.js but could not resolve the issue. I also tried loading the database manually by calling loadDatabase(function(err) {}) but without success.

Here is a snippet of code:

var Datastore = require('nedb'),  
..
..
..                                                               
myPath = "<User-Home>/.myApp/user.db",
db = new Datastore({ filename: myPath, autoload: true });
dg99
  • 5,456
  • 3
  • 37
  • 49
Test Test
  • 145
  • 1
  • 14

2 Answers2

1

Just in case you didn't insert the data into the .db file with an actual insert() command, make sure your entries have a _id field for each JSON row.

ivandov
  • 619
  • 8
  • 14
0

Are you sure that "/.myApp/user.db" is a valid path? You might try "./myApp/user.db" instead.

Michael Blankenship
  • 1,639
  • 10
  • 16
  • Yes, its hidden folder under user's home directory. '/Users/user-home/.myApp/user.db' (on mac). I'm able to write and I can see the data in file after inserting a document. But, on restart of app, it's cleaned/emptied/overwritten (it's happening where I'm creating DataStore) – Test Test Apr 03 '15 at 00:39
  • I tried with non-hidden folder but same issue. It works for someone as seen [here](http://stackoverflow.com/questions/19315550/how-to-achieve-persistent-storage-in-nedb) – Test Test Apr 03 '15 at 00:49
  • On further debugging, I am seeing that loadDatabase is creating data-file thereby causing loss of old data. Any clue? – Test Test Apr 03 '15 at 19:37
  • The docs suggest that if you use autoload then you don't manually call loadDatabase (which would in fact overwrite your file). Hopefully that code's not still in your project somewhere. – Michael Blankenship Apr 03 '15 at 22:57
  • You might try to name the WebKitApp as in... var Datastore = require('nedb'), db = new Datastore({filename: myPath, autoload: true, nodeWebkitAppName: 'nwtest' }); [or whatever your appname is] – Michael Blankenship Apr 03 '15 at 23:00
  • You might try that both with and without the autoload in it. – Michael Blankenship Apr 03 '15 at 23:01
  • Thanks Michael for reply. 1. No, I did't call loadDatabase when 'autoload:true' was used, I tried loadDatabase with manuall load. 2. I did try with WebKitApp name and using 'require('nw.gui').App.dataPath' as suggested because 'nodeWebkitAppName' flag is deprecated but same result - i can see data written to file but on restart of app, file is overwritten. – Test Test Apr 04 '15 at 00:01
  • Alright then... try reading this thread. http://stackoverflow.com/questions/19315550/how-to-achieve-persistent-storage-in-nedb – Michael Blankenship Apr 05 '15 at 00:04
  • Thanks @Michael. I have seen it. I found the problem. My method which was saving data to localstorage was used here to save data to NeDB. The issue was in method as it was trying to save string to localstorage, not an object or collection of objects. – Test Test Apr 05 '15 at 20:58