Node.js 猫鼬鉴别器:NodeJS/Express
我已经使用Express开发了crudapi,在其中一个createapi中,我希望在请求体中提供的布尔标志的基础上扩展我的Mongoose模型模式 创建API主体(需要以这种格式存储在数据库中):Node.js 猫鼬鉴别器:NodeJS/Express,node.js,mongodb,mongoose,discriminator,Node.js,Mongodb,Mongoose,Discriminator,我已经使用Express开发了crudapi,在其中一个createapi中,我希望在请求体中提供的布尔标志的基础上扩展我的Mongoose模型模式 创建API主体(需要以这种格式存储在数据库中): { "name" : "John Doe", "admin" : true, "adminControl1" : "Some Control", "adminControl2" : "Some Other Control", } 但我的MongoDB中存储的数据如下: { "n
{
"name" : "John Doe",
"admin" : true,
"adminControl1" : "Some Control",
"adminControl2" : "Some Other Control",
}
但我的MongoDB中存储的数据如下:
{
"name" : "John Doe",
"admin" : "true",
}
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
var options = { discriminatorKey: 'admin' }
var userSchema = new Schema({
name: {
type: String,
required: true
}
}, options);
const Event = mongoose.model('Event',userSchema);
const addedSchema = new mongoose.Schema({
adminControl1 : {
type: String
},
adminControl2 : {
type: String
},
},options);
const addedSchemaEvent = Event.discriminator(true,addedSchema);
module.exports = mongoose.model('Users', userSchema,'users');
我的模型文件如下:
{
"name" : "John Doe",
"admin" : "true",
}
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
var options = { discriminatorKey: 'admin' }
var userSchema = new Schema({
name: {
type: String,
required: true
}
}, options);
const Event = mongoose.model('Event',userSchema);
const addedSchema = new mongoose.Schema({
adminControl1 : {
type: String
},
adminControl2 : {
type: String
},
},options);
const addedSchemaEvent = Event.discriminator(true,addedSchema);
module.exports = mongoose.model('Users', userSchema,'users');
鉴别器的第一个参数是新的型号名称
这是我的模型的一个例子
let Area: any = mongoose.model("Area", areaSchema);
let Country: any = Area.discriminator("Country", countrySchema);
let City: any = Area.discriminator("City", citySchema);
module.exports = {
Area: Area,
Country: Country,
City: City,
};
其中areaSchema是基本模式,countrySchema和citySchema是添加的模式
我导入了这样的模型
const Area_ = require("../models/Area");
const Area = Area_.Area;
const City = Area_.City;
const Country = Area_.Country;
您可以共享整个model.js结构吗?我也应该导出事件吗?仍然无法通过。有什么秘密吗?