6

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.

2
  • I can't see the ObjectID.createFromHexString method anywhere in the docs. Have you tried simply ObjectId(<hexadecimal>), e.g. var id = ObjectId("507f1f77bcf86cd799439011")? Commented Jul 10, 2016 at 4:36
  • Search for createFromHexString in mongodb.github.io/node-mongodb-native/api-bson-generated/…. I posted what turned out to be the problem, below. Thanks. Commented Jul 10, 2016 at 4:38

2 Answers 2

4

Change mongodb version to 2.1.6.

Sign up to request clarification or add additional context in comments.

4 Comments

mongod --version shows: db version v3.0.7 git version: nogitversion
I mean change mongodb module version of nodejs to 2.1.6 or 2.1.17.The version has a bug when create objectID. @Chris Prince
I also had this problem except I am not using mongoose. Solution for me was to change version of mongodb driver. In my case 2.2.10 didn't work but 2.0.28 does. A link to the specific bug report would be great if anyone happens to know where to find it...
2.1.17 worked for me. This is odd. I had first "^2.1.17" in the package.json, and this would not work, as it would install a newer version, but the exact version is ok. This is weird, as it seems the node module is dragging around the bug in different versions....
2

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");
            });
        }
    });
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.