【问题标题】:Reusing mongoose existing schema model from one file to schema model from another file将一个文件中的 mongoose 现有模式模型重用到另一个文件中的模式模型
【发布时间】:2018-08-25 04:04:58
【问题描述】:

我正在使用两种不同类型的用户在 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 中使用相同的 UserSchema 和另一个属性 familyNo 我尝试了以下方法,但失败了。

 //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;

【问题讨论】:

    标签: javascript node.js mongodb mongoose mongoose-schema


    【解决方案1】:

    问题是你没有传递模式,你正在传递用户模型,将你的用户模式移动到不同的文件中,并将其用作两个模型中的模式,这将解决问题

    //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])
    

    用户.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
    });
    

    【讨论】:

    • 你能写代码来导入模式并在另一个模型中使用它吗?
    • 你读过我的整个代码吗?我知道如何导出模型,但无法在架构中使用它。
    • 你可以像上面一样使用它
    • 让我检查一下
    猜你喜欢
    • 2011-08-11
    • 2019-11-08
    • 2016-03-18
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多