[Bug - Mongoose >=4.7.3] Fail to create new record with string _id #4867

Closed
kyofight opened this Issue Jan 4, 2017 · 19 comments

Projects

None yet
@kyofight
kyofight commented Jan 4, 2017

When I try to create a new record with the "create" Model method by supplying the _id as a String,

Model.create({
      _id: "5788683c1fa46a472a051543"
     name: "abc"
});

it fails with the following error:
{"errors":{"_id":{"message":"Cast to ObjectID failed for value \"5788683c1fa46a472a051543\" at path \"_id\"","name":"CastError","stringValue":"\"5788683c1fa46a472a051543\"","kind":"ObjectID","value":"5788683c1fa46a472a051543","path":"_id","reason":{"message":"Cast to ObjectId failed for value \"5788683c1fa46a472a051543\" at path \"_id\"","name":"CastError","stringValue":"\"5788683c1fa46a472a051543\"","kind":"ObjectId","value":"5788683c1fa46a472a051543","path":"_id"}}},"message":"Model validation failed","name":"ValidationError"}

I suppose for Mongoose >= 4.7.3, it does not automatically convert string _id value to ObjectId _id value when we try to insert new records

@joetidee
joetidee commented Jan 4, 2017

What is your _id defined as in your schema?

@thedewpoint

Also having this issue when retrieving records with a string ID using findById.

(err): CastError: Cast to ObjectId failed for value "586c6806a6918640e144622a" at path "_id" for model "User"

@JvrBaena
JvrBaena commented Jan 4, 2017

I think this is related to the upgrade of the bson package. See here #4860

...Although I have tried installing all dependencies so we get the latest version of bson and the bug persist... :(

@raugaral
raugaral commented Jan 4, 2017

(err): CastError: Cast to ObjectId failed for value "586c6806a6918640e144622a" at path "_id" for model "User

I have the same problem, at the moment I have "solve" it using the version 4.7.2. All posterior version have this bug. To install the version 4.7.2 you can use this command:

npm install mongoose@4.7.2

@ifyour
ifyour commented Jan 4, 2017

@raugaral Thanks,it's work now!

@joetidee
joetidee commented Jan 4, 2017

Make sure that if using a string for the _id, that you also have the following defined in your schema:

_id: {type: String}

@thedewpoint
@JCBird1012

Seconding @thedewpoint above. The default for an unspecified _id in your schema is an ObjectId. Before 4.7.3, strings were automatically cast into ObjectIds across all Mongoose functions (findByID and others).

+1 for this issue.

@foriam foriam referenced this issue in mongodb/js-bson Jan 5, 2017
Closed

Fix error - hex is not a function #205

@foriam
foriam commented Jan 5, 2017 edited

ObjectId creation from a valid string fails. Can be reproduced in my case by:

require('mongoose').Types.ObjectId('AAAAAAAAAAAAAAAAAAAAAAAA');

It works if line 17 in node_modules/bson/lib/bson/objectid.js is changed to:

if (Buffer && Buffer.from('testString')) {
    hasBufferType = true;
}
@JvrBaena
JvrBaena commented Jan 5, 2017

Everything goes back to this issue in the bson package, I'm afraid: mongodb/js-bson#204

@JvrBaena JvrBaena referenced this issue in mongodb/js-bson Jan 5, 2017
Closed

Hex is not a function on ObjectId creation #204

@foriam
foriam commented Jan 5, 2017

It seems if you can update to node 7.4.0 the issue is gone, as the Buffer.from function is working as expected by bson, which is used in mongoose.

Test (working in 7.4.0, probably not working in your node version):

console.log(
    require('buffer').Buffer.from('AAAAAAAAAAAAAAAAAAAAAAAA', 'hex')
);
@varunjayaraman
Collaborator
varunjayaraman commented Jan 5, 2017 edited

I can't seem to reproduce this issue:

node v6.9.2
mongodb v3.2

Tried with mongoose 4.7.3, 4.7.4, 4.7.6

This is the script I used to attempt to reproduce:

// NPM Deps
const mongoose = require('mongoose');
const co = require('co');
const chalk = require('chalk');

// Constants
const GITHUB_ISSUE = `gh-4867`;
const STRING_AS_ID = '5788683c1fa46a472a051543';

// Set mongoose promise
mongoose.Promise = global.Promise;

try {
  exec();
} catch (error) {
  console.log(chalk.red(`Error: ${error}\n${error.stack}`));
}


function exec() {
  return co(function* () {
    mongoose.connect(`mongodb://localhost:27017/${GITHUB_ISSUE}`);
    
    const ThingSchema = new mongoose.Schema({
      name: String
    });

    const Thing = mongoose.model('Thing', ThingSchema);

    const thing = yield Thing.create({
      _id: STRING_AS_ID,
      name: 'Bob'
    });
    console.log(chalk.green(`thing exists with id ${ STRING_AS_ID }: ${ !!thing }`));

    const foundThing = yield Thing.findById(STRING_AS_ID);
    console.log(chalk.green(`thing exists after finding by string id ${ STRING_AS_ID }: ${ !!foundThing }`));
  }).catch(function(error) {
    console.error(chalk.red(`Error ${error}\n${error.stack}`));
  });
}

Does this script produce the same cast to ObjectId error?

@thedewpoint
@JvrBaena
JvrBaena commented Jan 5, 2017 edited

It definitely depends on the node version, as explained here mongodb/js-bson#204 ... I'm not sure there's anything that can be done from this side (mongoose)... as AFAIK the issue comes from the bson package, which in turn is a dependency of mongodb-core (listed in the package.json with ^ which would explain why doing npm install broke stuff for versions that worked before)

Im my case, it's node 4.4.4

This is the output of your script @varunjayaraman when run on my machine with mongoose 4.7.4 (bson 1.0.3) and node 4.4.4

Error ValidationError: CastError: Cast to ObjectID failed for value "5788683c1fa46a472a051543" at path "_id"
ValidationError: Thing validation failed
    at MongooseError.ValidationError (/Users/javi/work/socialbro/node_modules/mongoose/lib/error/validation.js:23:11)
    at model.Document.invalidate (/Users/javi/work/socialbro/node_modules/mongoose/lib/document.js:1486:32)
    at model.Document.set (/Users/javi/work/socialbro/node_modules/mongoose/lib/document.js:753:10)
    at model._handleIndex (/Users/javi/work/socialbro/node_modules/mongoose/lib/document.js:596:14)
    at model.Document.set (/Users/javi/work/socialbro/node_modules/mongoose/lib/document.js:556:24)
    at model.Document (/Users/javi/work/socialbro/node_modules/mongoose/lib/document.js:68:10)
    at model.Model (/Users/javi/work/socialbro/node_modules/mongoose/lib/model.js:47:12)
    at new model (/Users/javi/work/socialbro/node_modules/mongoose/lib/model.js:3238:13)
    at /Users/javi/work/socialbro/node_modules/mongoose/lib/model.js:1864:51
    at /Users/javi/work/socialbro/node_modules/mongoose/node_modules/async/internal/parallel.js:27:9
    at eachOfArrayLike (/Users/javi/work/socialbro/node_modules/mongoose/node_modules/async/eachOf.js:57:9)
    at exports.default (/Users/javi/work/socialbro/node_modules/mongoose/node_modules/async/eachOf.js:9:5)
    at _parallel (/Users/javi/work/socialbro/node_modules/mongoose/node_modules/async/internal/parallel.js:26:5)
    at parallelLimit (/Users/javi/work/socialbro/node_modules/mongoose/node_modules/async/parallel.js:85:26)
    at /Users/javi/work/socialbro/node_modules/mongoose/lib/model.js:1882:5
    at Function.create (/Users/javi/work/socialbro/node_modules/mongoose/lib/model.js:1852:17)

...the CastError is indeed a hex is not a function error coming from the bson package and being catched

@varunjayaraman
Collaborator

@JvrBaena thanks, seems like it fails up to but not including node v6.0 on mongoose >= 4.7.3.

I also agree that this isn't a mongoose issue, but a problem with js-bson (through mongodb-core). This issue will hopefully be resolved soon: mongodb/js-bson#205

@varunjayaraman varunjayaraman added this to the 4.8 milestone Jan 6, 2017
@varunjayaraman
Collaborator

For now the solution is to use mongoose < 4.7.2 or upgrade node >= 6.0. If anyone else has any other ideas, feel free to comment, but i think this and waiting for js-bson to merge in PR 205 are the best solutions.

@vkarpov15 vkarpov15 modified the milestone: 4.7.7, 4.8 Jan 6, 2017
@varunjayaraman varunjayaraman modified the milestone: 4.7.7, 4.10 Jan 6, 2017
@kapilgarg1996

Using node >=6.0 worked for me

@Airboy
Airboy commented Jan 6, 2017

I have this issue with node 6.9.3 and mongoose 4.7.6. But it works with mongoose 4.7.3.

@ronteq
ronteq commented Jan 10, 2017

Using node >=6.0 worked for me too (6.9.1)

@vkarpov15 vkarpov15 closed this in 066f128 Jan 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment