Error in type referencing mongoose schema
mongodb, mongoose, node.js
Solution
You have a typo in your userSchema. You've put
trips: [{Type: mongoose.Schema.Types.ObjectId, ref: 'Trip'}]
But it should be
trips: [{type: mongoose.Schema.Types.ObjectId, ref: 'Trip'}]
`Type` should be lowercase `type`
Problem
I am developing a node application with mongodb using mongoose ODM. I am getting an error while type referencing schemas that reside in different files. I have following code in user.js file: ``` var mongoose = require('mongoose'); var Trip = require('./trip'); var userSchema = mongoose.Schema({ firstName: String, lastName: String, salt: String, hash: String, emailAddress: { type: String, unique: true }, trips: [{Type: mongoose.Schema.Types.ObjectId, ref: 'Trip'}] }); module.exports = mongoose.model('User', userSchema); ``` userSchema has a type reference to tripSchema. Code in my trip.js file is: ``` var tripSchema = mongoose.Schema({ name: String, location: String, arrivalDate: Date, departureDate: Date, type: String}); module.exports = mongoose.model('Trip', tripSchema); ``` When I run the application, I am getting following error: ``` /usr/lib/node_modules/mongoose/lib/schema.js:360 throw new TypeError('Undefined type at `' + path + ^ TypeError: Undefined type at `trip.ref` Did you try nesting Schemas? You can only nest using refs or arrays. at Function.Schema.interpretAsType (/usr/lib/node_modules/mongoose/lib/schema.js:360:11) at Schema.path (/usr/lib/node_modules/mongoose/lib/schema.js:303:29) at Schema.add (/usr/lib/node_modules/mongoose/lib/schema.js:217:12) at Schema.add (/usr/lib/node_modules/mongoose/lib/schema.js:212:14) at new Schema (/usr/lib/node_modules/mongoose/lib/schema.js:73:10) at Function.Schema.interpretAsType (/usr/lib/node_modules/mongoose/lib/schema.js:345:44) at Schema.path (/usr/lib/node_modules/mongoose/lib/schema.js:303:29) at Schema.add (/usr/lib/node_modules/mongoose/lib/schema.js:217:12) at new Schema (/usr/lib/node_modules/mongoose/lib/schema.js:73:10) at Mongoose.Schema (/usr/lib/node_modules/mongoose/lib/schema.js:53:12) ``` I am unable to figure out the reason of error. If both the schemas are in same file, code is running fine. But when I seperate out schemas in two different files, I am getting above error. How can I resolve this error? Any help would be greatly appreciated.