Philippe Hebert
Philippe Hebert

Reputation: 2028

Mongoose bulkWrite - Wrong type for 'q'

For my test suite, I want to bulkWrite test info in the database, and then bulk delete any of the test info entered throughout the test to come back to a clean slate. I do so by running a bulkWrite on the db to which I pass the content of a JSON file loaded via nodeJS's require statement.

The problem is that for the dataset

[ { deleteOne: { username: 'test-author' } } ]

passed to models[key].collection.bulkWrite(action[key]), where key is the name of the model of interest and action is the JSON file,I get the following error:

{ MongoError: Wrong type for 'q'. Expected a object, got a null.
    at Function.MongoError.create (/var/www/website/server/node_modules/mongodb-core/lib/error.js:31:11)
    at /var/www/website/server/node_modules/mongodb-core/lib/connection/pool.js:483:72
    at authenticateStragglers (/var/www/website/server/node_modules/mongodb-core/lib/connection/pool.js:429:16)
    at Connection.messageHandler (/var/www/website/server/node_modules/mongodb-core/lib/connection/pool.js:463:5)
    at Socket.<anonymous> (/var/www/website/server/node_modules/mongodb-core/lib/connection/connection.js:339:20)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:548:20)
  name: 'MongoError',
  message: 'Wrong type for \'q\'. Expected a object, got a null.',
  ok: 0,
  errmsg: 'Wrong type for \'q\'. Expected a object, got a null.',
  code: 14,
  codeName: 'TypeMismatch' }

I have done some research and have been unable to find a solution to this problem. The error itself is pretty meaningless, so I can't grasp much out of it. Any idea how to solve the problem?

Any help would be greatly appreciated! Cheers!

Upvotes: 2

Views: 2250

Answers (1)

Philippe Hebert
Philippe Hebert

Reputation: 2028

As per the MongoDB API, the deleteOne, deleteMany, updateOne, updateMany, replaceOne, and replaceMany operation requires to have a property filter which acts as the filter for the query.

However Mongoose's API shows the following (mistaken) example:

Character.bulkWrite([
  ...
  {
    deleteOne: {
      { name: 'Eddard Stark' }
    }
  }
]).then(handleResult);

Hence, the data sent over changes from:

[{
  "deleteOne": { "username": "test-author" }
}]

to

[{
  "deleteOne": { "filter": { "username": "test-author" }} 
}]

I'll make sure to pass the message along to the mongoosejs dev group.

Upvotes: 2

Related Questions