Error handling in Node.js especially for bulkWrite method. Are docs wrong or am I wrong?

Hello everyone this is my first post here and I’m kinda new to mongodb (using for few months).

What I want to learn and try to understand is how can I handle errors in mongodb (Node.js driver) currently I’m trying to get the result member of BulkWriteResult which are:

hasWriteErrors method and getWriteErrors method. But they don’t exist in the result. I’m reading the driver docs here: BulkWriteResult | mongodb

And most of the members here are not defined/included in the result. I can use ok and it’s working but I also want to use getWriteErrors.

Anybody help about this? How should I handle errors in mongodb for bulkWrite or any other operation? (Using 6.3.0) Is there a preferred way of checking if operation succeed or not and get the info of why not if so.

Thanks!

He @loeix

It might help if you shared a sample of the code so we could see what you’re doing.

const { collection } = await connectionHandler(collectionId, suppressAuth);
        const { insertedCount, modifiedCount, insertedIds, ok, hasWriteErrors, getWriteErrors } = await collection.bulkWrite(
            bulkOperations,
            { readConcern: consistentRead === true ? "majority" : "local", ordered: true }
        );

       // hasWriteErrors and getWriteErrors are undefined. When defined code throws an error.

        if (ok) {
            // ...
            // handle succeed
        } else {
           // handle error
            throw Error(`WeivData - Error when saving items using bulkSave: inserted: ${insertedCount}, updated: ${modifiedCount}, ok: ${ok}`);
        }

Currently I have this and it’s working without any problem. I’m using TS with the library I build and TS says (also docs says the same) there are members like hasWriteErrors, getWriteErrors etc. in the result of the bulkWrite method. But in reality it doesn’t return anything like that.

I wonder if these members only returned when there is a write error or are these members no longer supported?

In general how should I check if the operation succeed correctly or not. And if not succeed how can I get the reason/s.

Thanks!