J.SL.
J.SL.

Reputation: 21

NeDB not storing to file despite logging everything perfect

I'm doing a small project for fun and wanted to try using a database for the first time on my own, so I went with NeDB.

As I'm trying it out, I run it on Node and call the route in Postman. Just for the test I put the insert in a get, and as I'm calling it, it logs the correct object to my terminal, but nothing ends up in the actual file where I want to save it.

Worth noting might be that when I require the path for the file, it auto-finds the folder, but not the file, despite it being there and throwing error if I do not explicity tell the require its "/test.db".

Any help is much appreciated!

let testpath = require('../data-storage/db/test.db')
const Datastore = require('nedb'); 
let db = new Datastore({filename: testpath});
let doc = { name: 'Boo' }

server.use(express.json());

server.get('/test', (req, res) => {

  db.insert(doc,  (err, newDoc) => {

    if (err){console.log(err)}
    if (newDoc){console.log(newDoc)}

    console.log(newDoc.name)
  })
  res.send("Hello there")
})

Note: I've found a lot of issues about when it happened to electron applications, but this is not that. So far it's just a server and nothing else.

Upvotes: 0

Views: 254

Answers (1)

J.SL.
J.SL.

Reputation: 21

After a nights sleep I realized what was wrong:

let testpath = require('../data-storage/db/test.db')

The key isn't asking for a file, but a filepath(string). I had received errors on the line earlier when not using require, but it must've been something else that I misunderstood.

So this little code snippet is all that needed changing:

let testpath = '../data-storage/db/test.db'

Upvotes: 1

Related Questions