【发布时间】:2018-07-11 20:28:07
【问题描述】:
假设您有一个用户方案,可用于创建名为 User 的基本模型。然后对于用户角色,您使用 mongoose 鉴别器来创建称为 Admin、Employee 和 Client 的继承模型。有没有办法以编程方式确定 User 模型有多少区分/继承/角色可用,以及可用的名称?
我的代码方面的问题:
文件:models/user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var options = {discriminatorKey: 'role'};
var userSchema = mongoose.Schema({
name: String,
email: String,
password: String,
},options);
var User = mongoose.model('User', userSchema);
var Client = User.discriminator("Client", mongoose.Schema({
Address : String,
Tax_identification : String,
Phone_number : String,
Internal_Remarks : String,
CRM_status : String,
Recent_contact : String,
}));
var Employee = User.discriminator("Employee",mongoose.Schema({
Staff_Id: String,
}));
module.exports = {User: User, Client: Client, Employee: Employee };
文件:controllers/usersController.js
var User = require('../models/user.js').User;
module.exports = {
registerRoutes: function(app){
app.get('user/create',this.userCreateCallback)
},
userCreateCallback: function(req,res){
//Get Available User Roles - The function below doesn't exist,
//Just what I hypothetically want to achieve:
User.geAvailableDiscriminators(function(err,roles){
res.render('user/create',{roles:roles})
});
}
};
我希望我能够表达我想要做的事情。也欢迎替代方法。
【问题讨论】:
标签: node.js express mongoose mongoose-schema discriminator