From 32d9ed34cc646d17e2e55525e5d4b3ba924d89bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20N=C4=9Bmec?= Date: Wed, 17 Nov 2021 13:18:03 +0100 Subject: [PATCH] Fix insertMany exception when empty array is used --- lib/richlist/storage/mongo.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/richlist/storage/mongo.js b/lib/richlist/storage/mongo.js index c944d36..0999979 100644 --- a/lib/richlist/storage/mongo.js +++ b/lib/richlist/storage/mongo.js @@ -191,13 +191,22 @@ class MongoStorage { } } + const insertManyCollection = (collection, data) => { + if(data.length === 0) { + return Promise.resolve() + } + // insertMany fails when data is empty array + // (node:1) UnhandledPromiseRejectionWarning: MongoError: Invalid Operation, no operations specified + return collection.insertMany(data) + } + // insert documents - return this.transactions.insertMany(txs.map(tx => ({ + return insertManyCollection(this.transactions, txs.map(tx => ({ hash: tx.hash, locktime: tx.locktime, version: tx.version }))).then(() => { - return this.outputs.insertMany(outputs); + return insertManyCollection(this.outputs, outputs); }).then(() => this.blocks.insertOne({ hash: block.hash, height: block.height,