louischatriot / nedb

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

NeDB not saving into file or calling back in Express #664

Closed IceBotYT closed 3 years ago

IceBotYT commented 3 years ago

Hello! I am writing a bulk import function for a password manager for myself and I have come across an issue.

There is an array of passwords to import and I'm using a forEach() method to iterate through each password to import. I call the insert function and everything just stops. No error, no callback, no saving to file. Here is my code:

const express = require('express')
const app = express()
const { encrypt, decrypt } = require('./crypto')
const Datastore = require('nedb')

app.post('/bulkimport', checkAuthenticated, (req, res) => {
  var passwords = JSON.parse(req.body.passwords)
  var dbForUser = new Datastore('./passwords/' + req._passport.session.user + '.db')
  passwords.forEach(password => {
    function doc(code, link, name, password) {
      this.code = code
      this.link = link
      this.name = name
      this.password = password
    }
    var entry = new doc(password.name, password.url, password.username, password.password)
    console.log(entry)
      console.log('before insert') // gets logged
      dbForUser.insert(entry, function(err, doc) {
        console.log('after insert') // doesn't get logged
        if (err) return res.status(500).send()
        console.log(doc)
      })
  });
})

Thanks for the help!

IceBotYT commented 3 years ago

Question moved to StackOverflow.