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.
The ref option is what tells Mongoose which model to use during population, in our case the Story model. All _id s we store here must be document _id s from the Story model. Note: ObjectId , Number , String , and Buffer are valid for use as refs.
Mongoose Schematype is a configuration for the Mongoose model. Before creating a model, we always need to create a Schema. The SchemaType specifies what type of object is required at the given path. If the object doesn't match, it throws an error.
ValidationError is generic error and is thrown by several other modules with each having different format and/or properties. Its better to be specific and check err instanceof mongoose. Error. ValidationError in your error checking procedure.
Each Schema can define instance and static methods for its model.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With