NeDB not saving into file with Express.JS

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!



from Recent Questions - Stack Overflow https://ift.tt/3oEmFSP
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Object oriented programming concepts (OOPs)

Spring Webflux : How to return HTTP 200 response with body when the called service returns HTTP 201 without body with WebClient?