node.js – 在另一个模型中为不同文件中的mongoose需要模型模式

我想在模型中要求我的模型.我确信我的要求路径是正确的,因为我的要求没有错误,但似乎每当我保存新实例时它都不会触发我的模型.我在这里错过了什么?

role.js

module.exports = function () {
    var mongoose = require('mongoose'),Schema = mongoose.Schema;

    var role = new Schema({
        type: String,level: Number,});

    role.pre("save",function(next) {
        this.type = 'member';
        this.level = 0;

        next();
    });

    return mongoose.model('Role',role);
}

user.js的

module.exports = function (connection) {
    var mongoose = require('mongoose');
        Role = require('./role.js'),Schema = mongoose.Schema;

    var user = new mongoose.Schema({
        first_name: String,last_name: String,email: String,password: String,profile_image: String,company_name: String,type: [{ type : String,default: 'Local' }],created_at: { type : Date,default: Date.Now },created_by: { type: Schema.Types.ObjectId,ref: 'User' },role: [Role]
    });

    return connection.model('User',user);
}

解决方法

您可以从模型实例直接访问底层模式,如下所示:
module.exports = function (connection) {
    var mongoose = require('mongoose'),Role = require('./role.js'),RoleSchema = mongoose.model('Role').schema,role: [RoleSchema]
    });

    return mongoose.model('User',user);
}

相关文章

这篇文章主要介绍“基于nodejs的ssh2怎么实现自动化部署”的...
本文小编为大家详细介绍“nodejs怎么实现目录不存在自动创建...
这篇“如何把nodejs数据传到前端”文章的知识点大部分人都不...
本文小编为大家详细介绍“nodejs如何实现定时删除文件”,内...
这篇文章主要讲解了“nodejs安装模块卡住不动怎么解决”,文...
今天小编给大家分享一下如何检测nodejs有没有安装成功的相关...