Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node mongodb: Error: connection closed due to parseError

Using the native 'mongodb' npm package, I'm receiving

Error: connection closed due to parseError

When making a very basic query:

 collections.myCollection.findOne({id: someID}, function (err, repo) {
    ...  
 })

The weird thing is, the exact same query has run before. Types are identical for each query, etc.

like image 997
mikemaccana Avatar asked Oct 23 '13 15:10

mikemaccana


People also ask

What is Mongoparse error?

An error used when attempting to parse a value (like a connection string)

How does MongoDB connect to node js database?

To connect a Node. js application to MongoDB, we have to use a library called Mongoose. mongoose. connect("mongodb://localhost:27017/collectionName", { useNewUrlParser: true, useUnifiedTopology: true });

How do I access MongoDB over HTTP on the native driver port?

MongoDB has an uncomplicated web-based central port at 28017 by default. At the default port of 27017, there is no HTTP access. The error port is used for native driver access, not HTTP traffic. To obtain MongoDB, you'll need to use a driver like the MongoDB essential driver for NodeJS.

Do MongoDB connections close automatically?

1.3 close() method in the Mongo database In the Mongo universe, the close() method instructs the server to close a cursor and free the associated server resources. The server will automatically close the cursors that have no remaining results and the cursors that have been idle for a time.


2 Answers

Answering my own question to hopefully stop the next person from tearing their hair out:

As noted here, this error message is useless and doesn't relate to the actual problem. The production Mongo driver throws away all errors in a catch block.

To find what the error actually is:

  • open your node_modules/mongodb

  • find server.js

  • look for mongoReply.parseBody

  • log the err to see something actually useful.

In my case:

ReferenceError: collection is not defined

Edit: Node MongoDB native 1.4 is now stable, and includes a fix to this bug.

like image 170
mikemaccana Avatar answered Oct 17 '22 01:10

mikemaccana


For people who are using mongoose instead mongodb can check in the following location for the same.

/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection

like image 1
Prasanth Jaya Avatar answered Oct 17 '22 00:10

Prasanth Jaya