NestJS - 如何自我引用 mongoDB 架构 @nestjs/mongoose?
Posted
技术标签:
【中文标题】NestJS - 如何自我引用 mongoDB 架构 @nestjs/mongoose?【英文标题】:NestJS - How to self reference a mongoDB Schema @nestjs/mongoose? 【发布时间】:2021-06-28 07:53:41 【问题描述】:我正在尝试使用 @nestjs/mongoose
包自引用 Checklist
架构。但它抛出了一个 ReferenceError。我花了几个小时寻找解决方案,但一无所获,如果有人知道自我参考的正确方法,请帮助我...
错误
ReferenceError: Cannot access 'Checklist' before initialization
at Object.<anonymous> (/home/farhantahir/Projects/bonsai-backend-api/dist/db/checklists/checklists.schema.js:24:31)
at Module._compile (internal/modules/cjs/loader.js:1176:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
at Module.load (internal/modules/cjs/loader.js:1040:32)
at Function.Module._load (internal/modules/cjs/loader.js:929:14)
at Module.require (internal/modules/cjs/loader.js:1080:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/home/farhantahir/Projects/bonsai-backend-api/dist/db/checklists/index.js:13:14)
at Module._compile (internal/modules/cjs/loader.js:1176:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
at Module.load (internal/modules/cjs/loader.js:1040:32)
at Function.Module._load (internal/modules/cjs/loader.js:929:14)
at Module.require (internal/modules/cjs/loader.js:1080:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/home/farhantahir/Projects/bonsai-backend-api/dist/db/index.js:14:14)
at Module._compile (internal/modules/cjs/loader.js:1176:30)
这是我的架构
import * as mongoose from "mongoose"
import Prop, Schema, SchemaFactory from "@nestjs/mongoose"
import shortid from "shortid"
import
CHECKLIST_VISIBILITY,
CHECKLIST_VISIBILITY_UNLISTED,
from "~common/constants"
import BaseItem from "../common/schemas/BaseItem"
import descriptionLengthValidation from "../validators"
import User from "../users"
export type ChecklistVisibility = "public" | "private" | "unlisted"
export type ChecklistItemDocument = ChecklistItem & mongoose.Document
export type ChecklistDocument = Checklist & mongoose.Document
@Schema( timestamps: true )
class ChecklistItem extends BaseItem
@Prop( type: mongoose.Schema.Types.ObjectId, ref: "Checklist" )
linkedList: Checklist
@Schema()
export class Subscriber
@Prop( type: mongoose.Schema.Types.ObjectId, ref: "User" )
user: User
@Prop( type: mongoose.Schema.Types.ObjectId, ref: "Checklist" )
subscribedChecklist
@Schema( timestamps: true )
export class Checklist
@Prop(
validate: descriptionLengthValidation,
)
description: string
@Prop( trim: true )
name: string
@Prop( type: mongoose.Schema.Types.ObjectId, ref: "Checklist" )
parent: Checklist // <== Self Reference
@Prop( default: shortid.generate, permanent: true, unique: true )
slug: string
@Prop( type: mongoose.Schema.Types.ObjectId, ref: "User" )
user: User
@Prop( default: CHECKLIST_VISIBILITY_UNLISTED, enum: CHECKLIST_VISIBILITY )
visibility: string
@Prop(
type: mongoose.Schema.Types.ObjectId,
ref: "Checklist",
)
subscribedFrom: Checklist // <== Self Reference
@Prop()
subscribedAt: Date
@Prop()
subscribers: [Subscriber]
@Prop()
items: [ChecklistItem]
@Prop()
trashedAt: Date
export const ChecklistSchema = SchemaFactory.createForClass(Checklist)
ChecklistSchema.pre("find", function ()
this.where( trashedAt: $exists: false )
)
【问题讨论】:
【参考方案1】:您将 Checklist 用于订阅者类,当时尚未声明 Checklist 类。你应该先声明 Checklist 类。
【讨论】:
以上是关于NestJS - 如何自我引用 mongoDB 架构 @nestjs/mongoose?的主要内容,如果未能解决你的问题,请参考以下文章
GraphQL 如何使用 Nestjs 或 type-graphql 设置查询 MongoDB ref 集合
如何在nestjs mongoose typescript中引用嵌套文档