Bo Mil
Bo Mil

Reputation: 41

NeDB not loading or storing to file

I cannot get the simplest example of NeDB to run properly. My code only works in-memory, persistence to file keeps failing without any error messages.

The error callbacks for the loaddatabase and insert events always pass a null reference as error, so no information there. Oddly it seems no one else has this issue, so I guess I'm missing something here. All help is much appreciated.

Here is the code:

var Datastore = require('nedb'), db = new Datastore({ filename: 'test.db' });

db.loadDatabase(function (err) {   
  alert(err); // err is null, with the autoload flag no error is thrown either
});

var doc = { hello: 'world'};

db.insert(doc, function (err, newDoc) {   
  alert(err); // err is null here as well. Doc will be in the memory storage but no persisted to file
});

Upvotes: 4

Views: 5818

Answers (3)

Mason England
Mason England

Reputation: 11

All I had to do to fix this was delete the .db file and let the program make one for me by running it one more time.

The other thing I did that could have fixed it was making sure my package.json had all the required information. this can be easily done with a quick "npm init" in the terminal.

Upvotes: 0

Grzegorz B.
Grzegorz B.

Reputation: 366

This question is quite old but since I had very similar problem I thought that I'll write my resolution for anyone facing similar issues.

In my case I was writing Electron app using electron-webpack as an application builder. It turns out that NeDB loaded by Webpack was running in browser mode without access to file system.

To get it working I had to change import statement from:

import DataStore from 'nedb';

to this:

const DataStore = require('nedb');

Also I had to add NeDB to Webpack configuration as external module (in package.json):

"electronWebpack": {
    "externals": {
        "nedb": "commonjs nedb"
    }
}

I have found this resolution on NeDB github page: https://github.com/louischatriot/nedb/issues/329

Upvotes: 0

flojupp
flojupp

Reputation: 89

Although this question is pretty old, I'd like to share my experience for anyone facing a similar issue.

  1. NeDB API does not allow JSON input. You have to put in a javascript object. When you use JSON input, no error is returned and nothing will be persisted.
  2. 'null' is returned as error in callback to signal that no problem occurred. When saving the first JSON document it is indexed with 'undefined' key, because NeDB calls 'key = obj[fieldname[0]]' which returns 'undefined', when the obj is just a (JSON) string. No error is returned unfortunately. Inserting a second document will cause a unique constraint violation error in the callback as the key 'undefined' has already been taken. Anyhow, nothing will be persisted.

Try

var Datastore = require('nedb'), db = new Datastore({ filename: 'test.db' });

db.loadDatabase(function (error) {   
  if (error) {
      console.log('FATAL: local database could not be loaded. Caused by: ' + error);
      throw error;
    }
    console.log('INFO: local database loaded successfully.');
});

// creating the object with new, just to make it clear.
// var doc = {hello: 'world'}; should work too.
function myDoc(greeting)
{
   this.hello=greeting;
}
var doc = new myDoc('world');

db.insert(doc, function (error, newDoc) {   
  if (error) {
    console.log('ERROR: saving document: ' + JSON.stringify(doc) + '. Caused by: ' + error);
    throw error;
  }    
  console.log('INFO: successfully saved document: ' + JSON.stringify(newDoc));
});

Maybe it helps someone. :)

Upvotes: 4

Related Questions