Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in type referencing mongoose schema

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.

like image 842
Raeesaa Avatar asked Jan 29 '14 08:01

Raeesaa


People also ask

What does REF do in Mongoose schema?

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.

What is Mongoose schema type?

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.

What is validation error in Mongoose?

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.

Can you define methods in a Mongoose schema?

Each Schema can define instance and static methods for its model.


1 Answers

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

like image 51
matthewtole Avatar answered Oct 01 '22 23:10

matthewtole