[英]NestJS - Embedding Nested Document with enum using mongoose
我正在尝试创建一个像这样的Schema结构,
import * as mongoose from 'mongoose';
import * as Stop from 'stop-model';
export const RouteSchema = new mongoose.Schema({
_id: String,
stop: [Stop],
type: { type: String, enum: ['INBOUND', 'OUTBOUND'] }
}, {
versionKey: false,
timestamps: { createdAt: 'createTime', updatedAt: 'updateTime' }
});
停止模型是一个接口
import { Document } from 'mongoose';
export interface Stop extends Document {
_id: String,
stopName: String,
type: StopType,
createTime: number,
updateTime: number
}
export enum StopType {
PARKING= 'PARKING',
WAYPOINT = 'WAYPOINT',
STOP = 'STOP'
}
但是,运行时出现以下错误
TypeError:
StopType.PARKING
处的未定义类型PARKING
您是否尝试嵌套架构? 您只能使用引用或数组进行嵌套。
我的目标是在“ Routes
集合中找到“ Stops
列表。
我也有StopSchema
定义的StopSchema
,
import * as mongoose from 'mongoose';
export const StopSchema = new mongoose.Schema({
_id: String,
stopName: String,
type: { type: String, enum: ['PARKING', 'WAYPOINT', 'STOP'] }
}, {
versionKey: false,
timestamps: { createdAt: 'createTime', updatedAt: 'updateTime' }
});
我不知道如何使用StopSchema
作为内部参考RouteSchema
。 (此答案的内容引用了Mongoose中的另一个架构 ,但使用了NestJS的方式)。
在猫鼬中,要引用架构中的其他架构,必须使用Schema.Types.ObjectId
。
因此,在您的情况下,代码如下所示:
import * as mongoose from 'mongoose';
export const RouteSchema = new mongoose.Schema({
_id: String,
stop: { type: [{type: Schema.Types.ObjectId, required: false }] },
type: { type: String, enum: ['INBOUND', 'OUTBOUND'] }
}, {
versionKey: false,
timestamps: { createdAt: 'createTime', updatedAt: 'updateTime' }
});
让我知道是否有帮助;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.