Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db.create UnhandledPromiseRejectionWarning: MongoError: a collection 'mytest.test' already exists even when it's new #310

Open
matburnham opened this issue Jul 20, 2020 · 3 comments

Comments

@matburnham
Copy link

matburnham commented Jul 20, 2020

If I try to create a new database and add a few entries to it, I get an UnhandledPromiseRejectionWarning.

This is the simplest script I can get to see the error (it does need to insert data):

const monk = require('monk');

require('dotenv').config();
const db = monk(process.env.MONGO_URI);

const test = async () => {
  var collection = db.create('test');

  var records = [1, 2, 3];

  const promises = records.map(a => {
    return collection.insert({
      name: a
    });
  });

  await Promise.all(promises);

  db.close();
};

test();

If the collection does not exist before running the script, I get the following error:

$ node src/test.js
(node:77118) UnhandledPromiseRejectionWarning: MongoError: a collection 'mytest.test' already exists
    at MessageStream.messageHandler (/home/mat/dev/mytest/node_modules/mongodb/lib/cmap/connection.js:261:20)
    at MessageStream.emit (events.js:198:13)
    at processIncomingData (/home/mat/dev/mytest/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/home/mat/dev/mytest/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at doWrite (_stream_writable.js:415:12)
    at writeOrBuffer (_stream_writable.js:399:5)
    at MessageStream.Writable.write (_stream_writable.js:299:11)
    at Socket.ondata (_stream_readable.js:710:20)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
(node:77118) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:77118) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The collection is created and the documents are successfully added.

If the collection does already exist, output is silent. The documents are added to the collection.

It's almost like it's operating backwards!

monk 7.3.0; mongodb 1:3.6.3-0ubuntu2.1 (yes, I need to shift to a newer version of Ubuntu to upgrade this)

@matburnham
Copy link
Author

Actually, even simpler test case:

const monk = require('monk');
const db = monk('mongodb://localhost/test');

const test = async () => {
  var collection = db.create('test');
  await collection.insert({name: 'a'});
  db.close();
};

test();

It looks like the implicit collection creation is getting muddled up with the earlier explicit one.

This time Ubuntu 20.04; monk 7.3.0; mongodb-org 4.2.8

@matburnham
Copy link
Author

And adding debugging adds a bit of info, but not much:

const monk = require('monk');
const db = monk('mongodb://localhost/test');
db.addMiddleware(require('monk-middleware-debug'))

const test = async () => {
  var collection = db.create('test', {}, {'safe':false});
  await collection.insert({name: 'a'});
  db.close();
};

test();
  monk:manager connection opened +0ms
  monk:manager emptying queries queue (2 to go) +1ms
  monk:query insert: [Circular] +0ms
(node:18801) UnhandledPromiseRejectionWarning: MongoError: a collection 'test.test' already exists
    at MessageStream.messageHandler (/home/mat/dev/mytest/node_modules/mongodb/lib/cmap/connection.js:263:20)
    at MessageStream.emit (events.js:198:13)
    at processIncomingData (/home/mat/dev/mytest/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/home/mat/dev/mytest/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at doWrite (_stream_writable.js:415:12)
    at writeOrBuffer (_stream_writable.js:399:5)
    at MessageStream.Writable.write (_stream_writable.js:299:11)
    at Socket.ondata (_stream_readable.js:710:20)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
(node:18801) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:18801) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  monk:query insert: {"name":"a","_id":"5f1de31f9a0d09497195284a"} +0ms

@aluto-ss
Copy link

same issue, have any solutions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants