Reusing mongoose existing schema model from one file to schema model from another file

66
March 16, 2018, at 6:26 PM

I am building a web application in Node with two different types of user. And both of them will have common as well as different properties. The problem is I am unable to use the common mongoose schema model in another model.

user.js is the common model with the following schema :

//Requiring Mongoose
const mongoose = require('mongoose');
//Creating a variable to store our Schemas
const Schema = mongoose.Schema;

//Create User Schema and Model
const UserSchema = new Schema({
    email: {
        type: String,
        required: [true, 'Email Field is required'],
        unique:true,
        match: /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/
    },
    name: {
        type: String,
        required: [true, 'Name field is required']
    },
    password: {
        type: String,
        required: [true, 'Please enter your password']
    },
    phoneNo: {
        type: String,
        required: [true, 'Phone No is required']
    }
//Create a user model which is going to represent our model in the database 
   and passing our above-created Schema
    const User = mongoose.model('user', UserSchema);
    //Exporting Models
    module.exports = User;

Now I want to use the same UserSchema in another model file rider.js with another property familyNo I have tried following way but it failed.

 //Requiring Mongoose
const mongoose = require('mongoose');
//Importing user Schema to remove the code redundancy
const userSchema = require('./user');
//Creating a variable to store our Schemas
const Schema = mongoose.Schema;
//Create Driver Schema and Model
const RiderSchema = new Schema({
    user: userSchema
    familyNo: {
        type: String,
        required: [true, 'Name field is required']
    }
});
//Create a rider model is going to represent our model in the database and passing our above-created Schema
const Rider = mongoose.model('rider', RiderSchema);
//Exporting Models
module.exports = Rider;
Answer 1

the problem is you are not passing the schema , you are passing the user model , move your userschema in different file , and use it as schema in both the models , that will solve the problem

//Create a user model which is going to represent our model in the database  and passing our above-created Schema
const User = mongoose.model('user', UserSchema);
//Exporting Models
module.exports = User; // Here is the problem, User is a model not schema

UserSchema.js

const UserSchema = mongoose.Schema([Your Common Schema])

User.js

var userSchema = require('./UserSchema');
module.exports = mongoose.model('User', userSchema);

OtherModel.js

var userSchema = require('./UserSchema');
module.exports = mongoose.model('OtherModel' , {
   property : String,
   user : userSchema
});
READ ALSO
Bodyparser behavior after second question mark

Bodyparser behavior after second question mark

I wrote a simple API which will return requestquery as a response

88
How to deploy my nodeJS application on wildfly server

How to deploy my nodeJS application on wildfly server

I have developed server Rest APIs using node and ExpressJSNow I want to deploy this application on wildfly server so that my Client application can access the APIs

163
Login using Google API | Angular 5 and Node.js [on hold]

Login using Google API | Angular 5 and Node.js [on hold]

How to integrate Google+ login authentication to our login page using Angular 5 and Nodejs?

115