我们可以将自定义数组定义为Mongoose模式类型吗?

时间:2022-03-19 19:00:48

Mongoose Schema Type

define the role array in a new file, use that role module as a schema type when defining a new schema

在新文件中定义角色数组,在定义新模式时将该角色模块用作模式类型

//define this role array 
   exports.module = {
        role:['admin','consumer','super_admin']
    };

//can we use the above role array as a schema type when defining schema
    var UserSchema = new mongoose.Schema({
      name: {
        type:String,
        required:true
      },
      profilePic:String,
      email:{
        type:String,
        unique: true,
        lowercase: true,
        required:true
      },
      role:{
        type: [userRole.module.role],
        required:true
      }
    });

2 个解决方案

#1


0  

You can find answer on either

你可以找到答案

#2


0  

To create a new schema type, you need to inherit from mongoose.SchemaType and add the corresponding property to mongoose. For creating your custom schema type follow this link :

要创建新的模式类型,您需要从mongoose.SchemaType继承并将相应的属性添加到mongoose。要创建自定义架构类型,请点击以下链接:

http://mongoosejs.com/docs/customschematypes.html

http://mongoosejs.com/docs/customschematypes.html

#1


0  

You can find answer on either

你可以找到答案

#2


0  

To create a new schema type, you need to inherit from mongoose.SchemaType and add the corresponding property to mongoose. For creating your custom schema type follow this link :

要创建新的模式类型,您需要从mongoose.SchemaType继承并将相应的属性添加到mongoose。要创建自定义架构类型,请点击以下链接:

http://mongoosejs.com/docs/customschematypes.html

http://mongoosejs.com/docs/customschematypes.html