Use arrow functions to preserve this

pull/5/head
eliot-akira 3 years ago
parent 24dd632042
commit 3729d4c86e
  1. 11
      lib/persistence.js

@ -195,12 +195,11 @@ class Persistence {
let corruptItems = 0
const lineStream = byline(rawStream)
const that = this
let length = 0
lineStream.on('data', function (line) {
lineStream.on('data', (line) => {
try {
const doc = model.deserialize(that.beforeDeserialization(line))
const doc = model.deserialize(this.beforeDeserialization(line))
if (doc._id) {
if (doc.$$deleted === true) delete dataById[doc._id]
else dataById[doc._id] = doc
@ -213,10 +212,10 @@ class Persistence {
length++
})
lineStream.on('end', function () {
lineStream.on('end', () => {
// A bit lenient on corruption
if (length > 0 && corruptItems / length > that.corruptAlertThreshold) {
const err = new Error('More than ' + Math.floor(100 * that.corruptAlertThreshold) + '% of the data file is corrupt, the wrong beforeDeserialization hook may be used. Cautiously refusing to start NeDB to prevent dataloss')
if (length > 0 && corruptItems / length > this.corruptAlertThreshold) {
const err = new Error('More than ' + Math.floor(100 * this.corruptAlertThreshold) + '% of the data file is corrupt, the wrong beforeDeserialization hook may be used. Cautiously refusing to start NeDB to prevent dataloss')
cb(err, null)
return
}

Loading…
Cancel
Save