我的Role.js看起来像这样:
const mongoose = require ("mongoose");
const roleSchema = mongoose.Schema({
role : Number,
description : String
});
module.exports = mongoose.model("Role", roleSchema);
在我的logic.js中,我尝试这样做:
const Role = require('./Collections/Role');
const role = Role.create({
role: 2,
description: 'Editor'
})
console.log(role);
我似乎不能得到它的权利。我发现了一对夫妇其他线程几乎类似的问题,但它没有帮助我。
我也试过
const Schema = mongoose.Schema;
const roleSchema = new Schema({ ...
...
const Role = mongoose.model("Role", roleSchema);
module.exports = Role;
错误
C:\Users\my_is\Programming\Project\node_modules\mongoose\lib\schema.js:1340
throw new TypeError(`Invalid schema configuration: \`${name}\` is not ` +
^
TypeError: Invalid schema configuration: `Model` is not a valid type at path `role`. See
at Schema.interpretAsType
at Schema.path
at Object.<anonymous> (C:\Users\my\Programming\Project\Collections\Admin.js:6:30)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
- ---------------我的管理员-------------
const mongoose = require ("mongoose");
const Role = require("./Role");
//const Role = require("./Role").schema;
const adminSchema = mongoose.Schema({
firstName : String,
lasteName : String,
email : String,
dateCreate : { type: Date, default : Date.now } ,
role : Role
})
module.exports = mongoose.model("Admin", adminSchema);
2条答案
按热度按时间6ojccjat1#
我注意到您在
adminSchema
中包含了整个Role
模型,而不仅仅是模式。要解决这个问题,请将role: Role
行更改为type: mongoose.Schema.Types.ObjectId
,并设置对角色的引用。希望这能起作用:)
rmbxnbpk2#
应该将roleSchema从role.js导出到admin.js,并在admin.js中设置角色类型
角色架构
它会起作用的。