"Argument must be a string" with a specific MongoDB object identifier in Node.js

I was getting an error similar to this in Node:

TypeError: Argument must be a string at TypeError (native) at Buffer.write (buffer.js:791:21) at serializeObjectId <snip> 

Conditions were some use of ObjectID when performing a search operation with MongoDB. Some uses of ObjectID caused this error, and some did not. The only thing that mattered was where the ObjectID came from. If it was extracted from an existing collection, it worked. If I generated it myself (for example, using ObjectID.createFromHexString), it failed as described above.

+5
source share
2 answers

Change mongodb version to 2.1.6.

+4
source

I spent several hours tracking this. The problem came down to my use of Mongoose. I used Mongoose schemas for some of my Collections and did not use Mongoose for others. Here is the file containing my problematic code:

 // Some common MongoDb operations and data. 'use strict'; var MongoClient = require('mongodb').MongoClient; var assert = require('assert'); var logger = require('./Logger'); var mongoose = require('mongoose'); // TODO: Take this out for production. mongoose.set('debug, true'); var PSSharingInvitations = require('./PSSharingInvitations') var connectedDb = null; // Call this just once, at the start of the server. // TODO: Need better error handling when can't initially connect. Right now have an ugly looking error when Mongo is not already started and we try to start our server. exports.connect = function(mongoDbURL) { MongoClient.connect(mongoDbURL, function(err, db) { assert.equal(null, err); if (!db) { logger.error("**** ERROR ****: Cannot connect to MongoDb database!"); } else { mongoose.connect(mongoDbURL); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function() { // SCHEMA's exports.SharingInvitation = PSSharingInvitations.buildSchema(mongoose); logger.info("Mongoose: Connected to MongoDb database"); }); connectedDb = db; logger.info("Mongo: Connected to MongoDb database"); } }); }; exports.db = function () { return connectedDb; }; // Call this just once, when the server shuts down. exports.disconnect = function() { }; 

The problem turned out to be direct:

 connectedDb = db; 

Where db was mongoose.connection . That is, I used mongoose.connection as my db for MongoDB collections that did not use Mongoose. This caused intermittent errors.

The revised (and still working!) Code is as follows:

 exports.connect = function(mongoDbURL) { MongoClient.connect(mongoDbURL, function(err, db) { assert.equal(null, err); if (!db) { logger.error("**** ERROR ****: Cannot connect to MongoDb database!"); } else { connectedDb = db; logger.info("Mongo: Connected to MongoDb database"); mongoose.connect(mongoDbURL); var connectedMongooseDb = mongoose.connection; connectedMongooseDb.on('error', console.error.bind(console, 'connection error:')); connectedMongooseDb.once('open', function() { // SCHEMA's exports.SharingInvitation = PSSharingInvitations.buildSchema(mongoose); logger.info("Mongoose: Connected to MongoDb database"); }); } }); }; 
+2
source

All Articles