diff --git a/lib/persistence.js b/lib/persistence.js index 0cd75f1..a60a80f 100755 --- a/lib/persistence.js +++ b/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 }