0

I call collection.find(someBadQuery) and I get an error from MongoDB. But it ends up as unhandled rejection. How to handle this rejection? As described in docs For find() MongoDB NodeJS driver returns FindCursor and not promise, therefore .catch(), or async try ... catch will not work. It is EventEmmiter, but there is no error event.

Is there any way how to track which .find() call caused the error?

The error is for example:

Unhandled rejection:  MongoServerError: E11000 duplicate key error collection: data.Users__UserRole index: title_1 dup key: { title: null }
    at ...\node_modules\mongodb\lib\operations\insert.js:53:33
    at ...\node_modules\mongodb\lib\cmap\connection_pool.js:277:25
    at ...
    at handleOperationResult (...\node_modules\mongodb\lib\sdam\server.js:335:20)
    at Connection.onMessage (...\node_modules\mongodb\lib\cmap\connection.js:222:9)
    at MessageStream.<anonymous> (...\node_modules\mongodb\lib\cmap\connection.js:63:60)
    at MessageStream.emit (node:events:513:28)
    at processIncomingData (...\node_modules\mongodb\lib\cmap\message_stream.js:132:20)
    at MessageStream._write (...\node_modules\mongodb\lib\cmap\message_stream.js:33:9)
    at writeOrBuffer (node:internal/streams/writable:391:12)
    at _write (node:internal/streams/writable:332:10)
    at MessageStream.Writable.write (node:internal/streams/writable:336:10)
    at Socket.ondata (node:internal/streams/readable:754:22)
    at Socket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at Socket.Readable.push (node:internal/streams/readable:228:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
    at TCP.callbackTrampoline (node:internal/async_hooks:130:17) {

So how can I prevent unhandled rejections and actually trace query which causes the error? Where should I put error handler? Thanks.

4
  • Cursor iterator is async. find initiates the search request, driver retrieves the data, and cursor makes it available on application level. What was the error? Commented Aug 18, 2022 at 16:24
  • Doesn't matter what was the error, I want to catch it and work further with it, but I cannot attach any error handler anywhere. Commented Aug 18, 2022 at 19:13
  • Well, it does, as it may help answering "where should you put" the try-catch. If you want to catch any error - process.on('uncaughtException', handler) but there is little you can do at this point but exit the process. I Commented Aug 19, 2022 at 7:40
  • Lol, that's exactly why the error matters. Please read what it says - " E11000 duplicate key error ... operations\insert.js:53:33" - it happens on insert, not find. You are trying to insert a new user with the same title as the existing one. Commented Aug 20, 2022 at 11:42

1 Answer 1

1

To get Promise you need to call .toArray() on that FindCursor.

const findResult = await collection.find({}).toArray();
console.log('Found documents =>', findResult);

as mentioned in docs

Sign up to request clarification or add additional context in comments.

8 Comments

But the error occurs sooner than toArray()
You can try this syntax to code the collection.find and cursor.toArray as two statements - and have the code in a try-catch block: Access Data from Cursor. Also, see Error Handling.
@MáriusRak can you share that error message & stacktrace
I've added the error to the question
@MáriusRak its duplicate key error, you have 2 or more Docs with same key (in this case UserRole db has 2 or more Doc with title = null). To fix this delete one or all of them having title=null, and make sure title is not null when you are creating new role.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.