louischatriot / nedb

The JavaScript Database, for Node.js, nw.js, electron and the browser
MIT License
13.49k stars 1.03k forks source link

how to load database in sync? #488

Open TangMonk opened 7 years ago

pi0 commented 7 years ago

@TangMonk I think it is impossible as Node fs IO is async by default. Possible solutions :

TangMonk commented 7 years ago

@pi0, thanks I am using Asnyc/Await to fix that

TangMonk commented 7 years ago

ES7 Asnyc/Await not work:

app.get('/devices', async (req, res) => {
  let result = await db.find({})
  res.send(result)
})

with error:

(node:53615) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Converting circular structure to JSON
(node:53615) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
pi0 commented 7 years ago

Try this:

app.get('/devices', async (req, res) => {
  let result = await db.find({})
  res.send(JSON.stringify(result))
})
TangMonk commented 7 years ago

@pi0 thanks , but not work.

  1. I think there is not necessary JSON.stringify(result), because using normal callback without JSON.stringify(result) works too.

  2. I think the problem is that nedb not support Promise, So Async/Await can not work normally #344

Now I am using nedb-promise instead

soullivaneuh commented 4 years ago

Is there any chance to have native promise support panned? It would be a great addition.

@TangMonk I am currently using FeathersJS with nedb out of the box and not yet very familiar with. Do I have to do some extra work to adapt the project with nedb-promise? Can both works separately? :thinking: