I was getting an error like this in Node:
TypeError: Argument must be a string
at TypeError (native)
at Buffer.write (buffer.js:791:21)
at serializeObjectId
<snip>
the conditions were some uses of ObjectID's when doing a find operation with MongoDB. Some uses of ObjectID's raised this error and some did not. The only thing that mattered was where the ObjectID came from. If it was pulled from an existing collection, it worked. If I generated it myself (e.g., using ObjectID.createFromHexString), it failed as above.
Change mongodb version to 2.1.6.
I spent several hours tracking this one down. The problem came down to my use of Mongoose. I was using Mongoose schemas for some of my Collections and not using Mongoose for others. Here's 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 the line:
connectedDb = db;
Where db was the mongoose.connection
. That is, I was using the mongoose.connection
as my db for MongoDB Collections that were not using Mongoose. This caused intermittent errors.
The revised (and so far 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");
});
}
});
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With