From 3729d4c86edd39eb21cf003e98902fc2d06ddd87 Mon Sep 17 00:00:00 2001 From: eliot-akira Date: Sun, 3 Oct 2021 03:55:39 +0200 Subject: [PATCH] Use arrow functions to preserve this --- lib/persistence.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) 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 }