Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.97k stars 3.84k forks source link

mongoose find error - BUG #5709

Closed KevinHaisol closed 7 years ago

KevinHaisol commented 7 years ago

Current Behavior: when using npm MEAN stack express restAPI - module mongoose@4.12.1 and running a query such as myModel.find({"_id": mongoose.Types.ObjectId("someid")}, someFields).populate(myPopulate) it throws an error:

If I run the query myModel.findById(mongoose.Types.ObjectId("someid")).populate(myPopulate) it works fine.

Note: myPopulate is something like:

     var myPopulate = [{
            path: 'subDocument',
            select: {"field1": 1, "field2": 1, "field3": 1, "field4": 1},
          },   
          {
            path: 'subDocument.field1',
            select: {"field1_1": 1, "field1_2": 1}
          }];

Note: subDocument contains objects that contain otherSubDocument items: Example: {_id:..., name..., field1: { type: mongoose.Schema.Types.ObjectId, ref: 'otherSubDocument ' }}

I was digging through the mongoose node-module looking at the stack trace and the error looks like it's trying to cast a this.cast({}). and isn't handled. Not sure why it's casting it. In file lib\schematype.js -> SchemaType.prototype._castForQuery=function(val). Below is more debugging with the stack trace.

Query for myKey: {"_id":"5915b7677781d541f88f8cbd"} Before the mongoose library find function is called.

Lib->Schema->objectid.js->castForQuery ->arguments: { '0': 5915b7677781d541f88f8cbd } Lib->Schema->objectid.js->castForQuery ->val: undefined lib -> schematype.js -> _castForQuery -> val Start: 5915b7677781d541f88f8cbd lib -> schematype.js -> _castForQuery -> val befor this.cast(val): 5915b7677781d541f88f8cbd Lib->Schema->objectid.js->castForQuery ->arguments: { '0': '$in', '1': [ 5915b6c87781d541f88f8c73 ] } Lib->Schema->objectid.js->castForQuery ->val: [ 5915b6c87781d541f88f8c73 ] Lib->Schema->objectid.js->castForQuery ->arguments: { '0': 5915b6c87781d541f88f8c73 } Lib->Schema->objectid.js->castForQuery ->val: undefined lib -> schematype.js -> _castForQuery -> val Start: 5915b6c87781d541f88f8c73 lib -> schematype.js -> _castForQuery -> val befor this.cast(val): 5915b6c87781d541f88f8c73 Lib->Schema->objectid.js->castForQuery ->arguments: { '0': '$in', '1': [ 5915b5a77781d541f88f8c44 ] } Lib->Schema->objectid.js->castForQuery ->val: [ 5915b5a77781d541f88f8c44 ] Lib->Schema->objectid.js->castForQuery ->arguments: { '0': 5915b5a77781d541f88f8c44 } Lib->Schema->objectid.js->castForQuery ->val: undefined lib -> schematype.js -> _castForQuery -> val Start: 5915b5a77781d541f88f8c44 lib -> schematype.js -> _castForQuery -> val befor this.cast(val): 5915b5a77781d541f88f8c44 Lib->Schema->objectid.js->castForQuery ->arguments: { '0': '$in', '1': [ 598b7ac8e66a0134b476da8b ] } Lib->Schema->objectid.js->castForQuery ->val: [ 598b7ac8e66a0134b476da8b ] Lib->Schema->objectid.js->castForQuery ->arguments: { '0': 598b7ac8e66a0134b476da8b } Lib->Schema->objectid.js->castForQuery ->val: undefined lib -> schematype.js -> _castForQuery -> val Start: 598b7ac8e66a0134b476da8b lib -> schematype.js -> _castForQuery -> val befor this.cast(val): 598b7ac8e66a0134b476da8b Lib->Schema->objectid.js->castForQuery ->arguments: { '0': '$in', '1': [ 598b7af9e66a0134b476da9e ] } Lib->Schema->objectid.js->castForQuery ->val: [ 598b7af9e66a0134b476da9e ] Lib->Schema->objectid.js->castForQuery ->arguments: { '0': 598b7af9e66a0134b476da9e } Lib->Schema->objectid.js->castForQuery ->val: undefined lib -> schematype.js -> _castForQuery -> val Start: 598b7af9e66a0134b476da9e lib -> schematype.js -> _castForQuery -> val befor this.cast(val): 598b7af9e66a0134b476da9e Lib->Schema->objectid.js->castForQuery ->arguments: { '0': '$in', '1': [ 598b19afa8093632449a1ed0 ] } Lib->Schema->objectid.js->castForQuery ->val: [ 598b19afa8093632449a1ed0 ] Lib->Schema->objectid.js->castForQuery ->arguments: { '0': 598b19afa8093632449a1ed0 } Lib->Schema->objectid.js->castForQuery ->val: undefined lib -> schematype.js -> _castForQuery -> val Start: 598b19afa8093632449a1ed0 lib -> schematype.js -> _castForQuery -> val befor this.cast(val): 598b19afa8093632449a1ed0 Lib->Schema->objectid.js->castForQuery ->arguments: { '0': '$in', '1': [ {}, { userId: 58da94d24feefc6310e3209f }, {}, {}, {}, {}, {}, { userId: 5845f8cf3b69f2a4c85fd385 }, {}, {}, {}, {}, {}, {}, { userId: 5915b7187781d541f88f8c96 }, {}, {}, { userId: 5915b5d67781d541f88f8c52 }, {}, {} ] } Lib->Schema->objectid.js->castForQuery ->val: [ {}, { userId: 58da94d24feefc6310e3209f }, {}, {}, {}, {}, {}, { userId: 5845f8cf3b69f2a4c85fd385 }, {}, {}, {}, {}, {}, {}, { userId: 5915b7187781d541f88f8c96 }, {}, {}, { userId: 5915b5d67781d541f88f8c52 }, {}, {} ] Lib->Schema->objectid.js->castForQuery ->arguments: { '0': {} } Lib->Schema->objectid.js->castForQuery ->val: undefined lib -> schematype.js -> _castForQuery -> val Start: {} lib -> schematype.js -> _castForQuery -> val befor this.cast(val): {} before check value before: {} Cast Error: {} Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters at new ObjectID (D:\GitHub\mySolution\node_modules\bson\lib\bson\objectid.js:50:11) at ObjectId.cast (D:\GitHub\mySolution\node_modules\mongoose\lib\schema\objectid.js:158:14) at ObjectId.SchemaType._castForQuery (D:\GitHub\mySolution\node_modules\mongoose\lib\schematype.js:1066:15) at ObjectId.castForQuery (D:\GitHub\mySolution\node_modules\mongoose\lib\schema\objectid.js:204:15) at D:\GitHub\mySolution\node_modules\mongoose\lib\schematype.js:991:18 at Array.map (native) at ObjectId.handleArray (D:\GitHub\mySolution\node_modules\mongoose\lib\schematype.js:990:14) at ObjectId.castForQuery (D:\GitHub\mySolution\node_modules\mongoose\lib\schema\objectid.js:201:20) at ObjectId.SchemaType.castForQueryWrapper (D:\GitHub\mySolution\node_modules\mongoose\lib\schematype.js:1016:17) at cast (D:\GitHub\mySolution\node_modules\mongoose\lib\cast.js:250:39) at Query.cast (D:\GitHub\mySolution\node_modules\mongoose\lib\query.js:3204:12) at Query._castConditions (D:\GitHub\mySolution\node_modules\mongoose\lib\query.js:1203:10) at Query._find (D:\GitHub\mySolution\node_modules\mongoose\lib\query.js:1218:8) at D:\GitHub\mySolution\node_modules\kareem\index.js:250:8 at D:\GitHub\mySolution\node_modules\kareem\index.js:23:7 at _combinedTickCallback (internal/process/next_tick.js:73:7) Lib->Schema->objectid.js->castForQuery ->arguments: { '0': '$in', '1': [ 59d78699a1dc642178b7f323 ] } Lib->Schema->objectid.js->castForQuery ->val: [ 59d78699a1dc642178b7f323 ] Lib->Schema->objectid.js->castForQuery ->arguments: { '0': 59d78699a1dc642178b7f323 } Lib->Schema->objectid.js->castForQuery ->val: undefined lib -> schematype.js -> _castForQuery -> val Start: 59d78699a1dc642178b7f323 lib -> schematype.js -> _castForQuery -> val befor this.cast(val): 59d78699a1dc642178b7f323 Lib->Schema->objectid.js->castForQuery ->arguments: { '0': '$in', '1': [ 58da94d24feefc6310e3209f, 5845f8cf3b69f2a4c85fd385, 5915b7187781d541f88f8c96, 5915b5d67781d541f88f8c52 ] } Lib->Schema->objectid.js->castForQuery ->val: [ 58da94d24feefc6310e3209f, 5845f8cf3b69f2a4c85fd385, 5915b7187781d541f88f8c96, 5915b5d67781d541f88f8c52 ] Lib->Schema->objectid.js->castForQuery ->arguments: { '0': 58da94d24feefc6310e3209f } Lib->Schema->objectid.js->castForQuery ->val: undefined lib -> schematype.js -> _castForQuery -> val Start: 58da94d24feefc6310e3209f lib -> schematype.js -> _castForQuery -> val befor this.cast(val): 58da94d24feefc6310e3209f Lib->Schema->objectid.js->castForQuery ->arguments: { '0': 5845f8cf3b69f2a4c85fd385 } Lib->Schema->objectid.js->castForQuery ->val: undefined lib -> schematype.js -> _castForQuery -> val Start: 5845f8cf3b69f2a4c85fd385 lib -> schematype.js -> _castForQuery -> val befor this.cast(val): 5845f8cf3b69f2a4c85fd385 Lib->Schema->objectid.js->castForQuery ->arguments: { '0': 5915b7187781d541f88f8c96 } Lib->Schema->objectid.js->castForQuery ->val: undefined lib -> schematype.js -> _castForQuery -> val Start: 5915b7187781d541f88f8c96 lib -> schematype.js -> _castForQuery -> val befor this.cast(val): 5915b7187781d541f88f8c96 Lib->Schema->objectid.js->castForQuery ->arguments: { '0': 5915b5d67781d541f88f8c52 } Lib->Schema->objectid.js->castForQuery ->val: undefined lib -> schematype.js -> _castForQuery -> val Start: 5915b5d67781d541f88f8c52 lib -> schematype.js -> _castForQuery -> val befor this.cast(val): 5915b5d67781d541f88f8c52 error { CastError: Cast to ObjectId failed for value "{}" at path "_id" for model "myModel" at MongooseError.CastError (D:\GitHub\mySolution\node_modules\mongoose\lib\error\cast.js:27:11) at ObjectId.cast (D:\GitHub\mySolution\node_modules\mongoose\lib\schema\objectid.js:161:13) at ObjectId.SchemaType._castForQuery (D:\GitHub\mySolution\node_modules\mongoose\lib\schematype.js:1066:15) at ObjectId.castForQuery (D:\GitHub\mySolution\node_modules\mongoose\lib\schema\objectid.js:204:15) at D:\GitHub\mySolution\node_modules\mongoose\lib\schematype.js:991:18 at Array.map (native) at ObjectId.handleArray (D:\GitHub\mySolution\node_modules\mongoose\lib\schematype.js:990:14) at ObjectId.castForQuery (D:\GitHub\mySolution\node_modules\mongoose\lib\schema\objectid.js:201:20) at ObjectId.SchemaType.castForQueryWrapper (D:\GitHub\mySolution\node_modules\mongoose\lib\schematype.js:1016:17) at cast (D:\GitHub\mySolution\node_modules\mongoose\lib\cast.js:250:39) at Query.cast (D:\GitHub\mySolution\node_modules\mongoose\lib\query.js:3204:12) at Query._castConditions (D:\GitHub\mySolution\node_modules\mongoose\lib\query.js:1203:10) at Query._find (D:\GitHub\mySolution\node_modules\mongoose\lib\query.js:1218:8) at D:\GitHub\mySolution\node_modules\kareem\index.js:250:8 at D:\GitHub\mySolution\node_modules\kareem\index.js:23:7 at _combinedTickCallback (internal/process/next_tick.js:73:7) message: 'Cast to ObjectId failed for value "{}" at path "_id" for model "myModel"', name: 'CastError', stringValue: '"{}"', kind: 'ObjectId', value: {}, path: '_id', reason: undefined, model: { [Function: model] hooks: Kareem { _pres: [Object], _posts: {} }, base: Mongoose { connections: [Object], models: [Object], modelSchemas: [Object], options: [Object], plugins: [Object] }, modelName: 'myModel', model: [Function: model], db: NativeConnection { base: [Object], collections: [Object], models: [Object], config: [Object], replica: false, hosts: null, host: 'localhost', port: 27017, user: undefined, pass: undefined, name: 'test', options: [Object], otherDbs: [], states: [Object], _readyState: 1, _closeCalled: false, _hasOpened: true, _listening: false, db: [Object] }, discriminators: undefined, '$appliedMethods': true, '$appliedHooks': true, _events: { init: [Function], save: [Function] }, _eventsCount: 2, schema: Schema { obj: [Object], paths: [Object], aliases: {}, subpaths: {}, virtuals: [Object], singleNestedPaths: {}, nested: [Object], inherits: {}, callQueue: [Object], _indexes: [], methods: [Object], statics: {}, tree: [Object], query: {}, childSchemas: [Object], plugins: [Object], s: [Object], options: [Object], '$globalPluginsApplied': true }, collection: NativeCollection { collection: [Object], opts: [Object], name: 'myModels', collectionName: 'myModels', conn: [Object], queue: [], buffer: false, emitter: [Object] }, Query: { [Function] base: [Object] }, '$__insertMany': [Function], insertMany: [Function], '$init': Promise { emitter: [Object], emitted: [Object], ended: false } } }

Expected Behavior: In mongoose@4.10.8 it does not throw this error and everything works fine.

It looks like in the new version it's not finding the value I'm passing in for the _id to find.

Mongoose DB version is: 3.4

KevinHaisol commented 7 years ago

Rewriting the query fixed it.