简体   繁体   中英

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

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;

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
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM