Mongoose: The latest mongoose version (5.10.8) throws a schema error

Created on 7 Oct 2020  路  6Comments  路  Source: Automattic/mongoose

Do you want to request a feature or report a bug?

bug

What is the current behavior?

The app throws an error when trying to use the schema on connection.model().

Invalid schema configuration: `undefined` is not a valid type at path `id`. See http://bit.ly/mongoose-schematypes for a list of valid schema types.

If the current behavior is a bug, please provide the steps to reproduce.

When a connection is made and trying to use an schema on a model function.

_Example code:_

const list = connection => {
  const Object = connection.model('ObjectsV2', objectSchema, 'objects');
  return Object.find()
};

_Logs:_

{"level":30,"time":1602076740140,"msg":"Invalid schema configuration: `undefined` is not a valid type at path `id`. See http://bit.ly/mongoose-schematypes for a list of valid schema types.","pid":27072,"hostname":"RFUNES-LAPTOP","reqId":1,"res":{"statusCode":400},"err":{"type":"TypeError","message":"Invalid schema configuration: `undefined` is not a valid type at path `id`. See http://bit.ly/mongoose-schematypes for a list of valid schema types.","stack":"TypeError: Invalid schema configuration: `undefined` is not a valid type at path `id`. See http://bit.ly/mongoose-schematypes for a list of valid schema types.\n    at Schema.interpretAsType (C:\\Project\\api\\src\\node_modules\\mongoose\\lib\\schema.js:1010:11)\n    at Schema.path (C:\\Project\\api\\src\\node_modules\\mongoose\\lib\\schema.js:686:27)\n    at Schema.add (C:\\Project\\api\\src\\node_modules\\mongoose\\lib\\schema.js:496:12)\n    at merge (C:\\Project\\api\\src\\node_modules\\mongoose\\lib\\helpers\\schema\\merge.js:4:6)\n    at Schema.add (C:\\Project\\api\\src\\node_modules\\mongoose\\lib\\schema.js:454:5)\n    at new Schema (C:\\Project\\api\\src\\node_modules\\mongoose\\lib\\schema.js:127:10\api\\src\\node_modules\\mong)\n    at Mongoose.model (C:\\Project\\api\\src\\node_modules\\mongoose\\lib\\index.js:489:14)\n    at NativeConnection.Connection.model (C:\\Project\\api\\src\\node_modules\\mongoose\\lib\\connection.js:1165:23)\n    at C:\\Project\\api\\src\\v2\\services\\mongoobjectstorage.js:147:21\n    at withapi\\src\\v2\\handlers\\objectsHanQuery (C:\\Project\\api\\src\\v2\\services\\queryBuilder.js:114:23)\n    at Object.list (C:\\Project\\api\\src\\v2\\handlers\\objectsHangine\\api\\src\\node_modules\\fastify\\libndler.js:150:12)\n    at Object.<anonymous> (C:\\Project\\api\\src\\v2\\features\\objects\\routes.js:103:8)\n    at preHandlerCallback (C:\\Project\\api\\src\\node_modules\\fastify\\lib\\api\\src\\node_modules\\fastify\\lib\\handleRequest.js:95:30)\n    at next (C:\\Project\\api\\src\\node_modules\\fastify\\lib\\hookRunner.js:8:7)\n   
 at handleResolve (C:\\Project\\api\\src\\node_modules\\fastify\\lib\\hookRunner.js:19:5)\n    at processTicksAndRejections (internal/process/task_queues.js:97:5)"}:27072,"hostname":"RFUNES-LAPTOP","reqId":1,"res","v":1}                                                                                                                                                                                            types.","stack":"Error: Invalid schema configurat
{"level":30,"time":1602076740265,"msg":"Invalid schema configuration: `undefined` is not a valid type at path `id`. See http://bit.ly/mongoose-schematypes for a list of valid schema types.","pid"er.js:346:18\n    at handleError (C:\\Project\:27072,"hostname":"RFUNES-LAPTOP","reqId":1,"res":{"statusCode":400},"err":{"type":"Error","message":"Invalid schema configuration: `undefined` is not a valid type at path `id`. See http://bit.lyply.js:68:5)\n    at C:\\Project\\api\\/mongoose-schematypes for a list of valid schema types.","stack":"Error: Invalid schema configuration: `undefined` is not a valid type at path `id`. See http://bit.ly/mongoose-schematypes for a list of valid schema types.\n    at C:\\Project\\api\\src\\buildServer.js:346:18\n    at handleError (C:\\Project\\api\\src\\node_modules\\fastify\\lib\\reply.js:333:18)\n    at _Reply.Reply.send (C:\\Project\\api\\src\\node_modules\\fastify\\lib\\reply.js:68:5)\n    at C:\\Project\\api\\src\\v2\\features\\objects\\routes.js:105:55\n    at processTicksAndRejections (internal/process/task_queues.js:97:5)"},"v":1}
{"level":30,"time":1602076740289,"msg":"request completed","pid":27072,"hostname":"RFUNES-LAPTOP","reqId":1,"res":{"statusCode":400},"responseTime":208.16920006275177,"v":1}

What is the expected behavior?

In mongoose version 5.10.7 the schema used was working as expected.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: v12.13
Mongoose: v5.10.8
MongoDB: v4.0.5

needs repro script

Most helpful comment

I was able to repro this based on @Adam-Edry 's suggestion. The problem here is multiple Mongoose modules: if you end up with two different Mongoose directories when you run npm install, schemas generated by one directory end up causing trouble when you try to instantiate a model using a different copy of Mongoose. We fixed this particular issue, but this use case is exceedingly difficult for us to test automatically, so if you define your schemas in a separate module, I would recommend listing Mongoose in peerDependencies in that separate module.

All 6 comments

What does your schema look like?

Hi @vkarpov15 . The schema looks like this:

const Schema = require('mongoose').Schema;

const schema = new Schema({
  field1: String,
  field2: String,
  field3: String,
  field4: String,
  field5: Object,
  field6: Object,
  field7: Boolean,
  field8: String,
  field9: Object,
  field10: [Object],
  field11: String,
  field12: String,
  field13: String,
  field14: String,
  field15: String,
  field16: Date,
  field17: Object,
  field18: Number
});

schema.virtual('id').get(function() {
  return this._id.toHexString();
});

I'm seeing this issue too with a virtual field starting from 5.10.8
Again I think this is due to having two mongoose modules in your installs similar to what I described in #9449
Using a peer dependency as vkarpov15 mentioned in that issue prevents this problem.

I haven't tried but I believe my repro script from that issue with the addition of a virtual field would reproduce this issue as well.

Other versions are same as before.
MongoDB: 4.2.9
Node.js: 10.16.3

I was able to repro this based on @Adam-Edry 's suggestion. The problem here is multiple Mongoose modules: if you end up with two different Mongoose directories when you run npm install, schemas generated by one directory end up causing trouble when you try to instantiate a model using a different copy of Mongoose. We fixed this particular issue, but this use case is exceedingly difficult for us to test automatically, so if you define your schemas in a separate module, I would recommend listing Mongoose in peerDependencies in that separate module.

Quite unlucky to bump into this twice in such a short time frame. Or maybe it was lucky since it was still fresh in our minds? :)

Thanks for de fix, @vkarpov15 . I'll try to use peerDependencies as you recommended. Also thanks to @Adam-Edry to provide the repro script :)

Was this page helpful?
0 / 5 - 0 ratings