将一个文件中的猫鼬现有模式模型重用到另一个文件中的模式模型
Reusing mongoose existing schema model from one file to schema model from another file
我正在用两种不同类型的用户在 Node 中构建一个 Web 应用程序。它们都将具有共同和不同的属性。
问题是我无法在另一个模型中使用常见的猫鼬模式模型。
user.js 是具有以下架构的通用模型:
//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;
现在我想在另一个模型文件 rider.js 和另一个 属性 familyNo 中使用相同的 UserSchema
我尝试了以下方法但失败了。
//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;
问题是你没有传递模式,你传递的是用户模型,将你的用户模式移动到不同的文件中,并在两个模型中将其用作模式,这将解决问题
//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
});
我正在用两种不同类型的用户在 Node 中构建一个 Web 应用程序。它们都将具有共同和不同的属性。 问题是我无法在另一个模型中使用常见的猫鼬模式模型。
user.js 是具有以下架构的通用模型:
//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;
现在我想在另一个模型文件 rider.js 和另一个 属性 familyNo 中使用相同的 UserSchema 我尝试了以下方法但失败了。
//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;
问题是你没有传递模式,你传递的是用户模型,将你的用户模式移动到不同的文件中,并在两个模型中将其用作模式,这将解决问题
//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
});