我有一个以JSON返回的对象数组,每个对象看起来都是这样的:
{
"reviewId": "f1a0ec26-9aca-424f-8b05-cff6aa3d2337",
"authorName": "Some name",
"comments": [
{
"userComment": {
"text": "tAmazing!",
"lastModified": {
"seconds": "1659685904",
},
"starRating": 5,
"reviewerLanguage": "en",
}
},
{
"developerComment": {
"text": "Thank you.",
"lastModified": {
"seconds": "1659688852",
}
}
}
]
}
我正试图为这个特定的对象创建一个Schema,但我遇到了一些问题,我无法理解如何创建它,这就是我迄今为止所做的:
import mongoose, { Document, Mongoose } from 'mongoose';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
export type ReviewDocument = Review & Document;
@Schema()
export class Review {
@Prop({ type: String, required: true })
reviewId: string;
@Prop({ type: String })
authorName: string;
@Prop({ type: mongoose.Schema.Types.Array })
comments: Comments[];
}
@Schema()
export class Comments {
@Prop({ type: mongoose.Schema.Types.ObjectId })
userComment: UserComment;
@Prop({ type: mongoose.Schema.Types.ObjectId })
developerComment: DeveloperComment;
}
@Schema()
export class UserComment {
@Prop({ type: String })
text: string;
@Prop({ type: String })
originalText: string;
@Prop({ type: String })
lastModified: string;
@Prop({ type: Number })
starRating: number;
@Prop({ type: String })
reviewerLanguage: string;
}
@Schema()
export class DeveloperComment {
@Prop({ type: String })
text: string;
@Prop({ type: String })
lastModified: string;
}
export const ReviewSchema = SchemaFactory.createForClass(Review);
它给了我一个错误:
/.../rtr-backend/src/schemas/reviews.schema.ts:21
userComment: UserComment;
^
ReferenceError: Cannot access 'UserComment' before initialization
at Object.<anonymous> (/.../rtr-backend/src/schemas/reviews.schema.ts:21:18)
at Module._compile (node:internal/modules/cjs/loader:1120:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1174:10)
at Module.load (node:internal/modules/cjs/loader:998:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
at Module.require (node:internal/modules/cjs/loader:1022:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/.../rtr-backend/src/reviews/reviews.module.ts:6:1)
at Module._compile (node:internal/modules/cjs/loader:1120:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1174:10)
什么是最佳实践?
在定义猫鼬模式时,需要注意以下几点:
- 基元属性的模式类型(例如
string
、number
(是自动推断的,因此不需要在decorator中显式指定{ type: String}
- 为了保留嵌套的模式验证,每个对象都必须有自己的模式,或者使用
raw
模式定义进行定义 - 对于每个嵌套模式,请注意mongose创建的默认属性,如
timestamps
、_id
、__v
等。您可以通过在@Schema()
装饰器中传递options对象来操作它们
以下是您的用例的模式定义:
import { Prop, raw, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
@Schema({ _id: false })
class UserComment {
@Prop({ required: true })
text: string;
@Prop(raw({ seconds: { type: Number } }))
lastModified: Record<string, number>;
@Prop({ required: true })
starRating: number;
@Prop({ required: true })
reviewerLanguage: string;
}
const UserCommentSchema = SchemaFactory.createForClass(UserComment);
@Schema({ _id: false })
class DeveloperComment {
@Prop({ required: true })
text: string;
@Prop(raw({ seconds: { type: Number } }))
lastModified: Record<string, number>;
}
const DeveloperCommentSchema = SchemaFactory.createForClass(DeveloperComment);
@Schema({ _id: false })
class Comment {
@Prop({ type: UserCommentSchema })
userComment?: UserComment;
@Prop({ type: DeveloperCommentSchema })
developerComment?: DeveloperComment;
}
const CommentSchema = SchemaFactory.createForClass(Comment);
@Schema({ timestamps: true, versionKey: false })
export class Review {
_id: Types.ObjectId;
createdAt: Date;
updatedAt: Date;
@Prop({ unique: true, required: true })
reviewId: string;
@Prop({ required: true })
authorName: string;
@Prop({ type: [CommentSchema], default: [] })
comments: Comment[];
}
export type ReviewDocument = Review & Document;
export const ReviewSchema = SchemaFactory.createForClass(Review);
附言:在这个文档页面中,你可以找到很多用例:https://docs.nestjs.com/techniques/mongodb
我认为您需要首先按照依赖关系的顺序定义模式,如下所示:
import mongoose, { Document, Mongoose } from 'mongoose';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
export type ReviewDocument = Review & Document;
@Schema()
export class UserComment {
@Prop({ type: String })
text: string;
@Prop({ type: String })
originalText: string;
@Prop({ type: String })
lastModified: string;
@Prop({ type: Number })
starRating: number;
@Prop({ type: String })
reviewerLanguage: string;
}
@Schema()
export class DeveloperComment {
@Prop({ type: String })
text: string;
@Prop({ type: String })
lastModified: string;
}
@Schema()
export class Comments {
@Prop({ type: mongoose.Schema.Types.ObjectId })
userComment: UserComment;
@Prop({ type: mongoose.Schema.Types.ObjectId })
developerComment: DeveloperComment;
}
@Schema()
export class Review {
@Prop({ type: String, required: true })
reviewId: string;
@Prop({ type: String })
authorName: string;
@Prop({ type: mongoose.Schema.Types.Array })
comments: Comments[];
}
export const ReviewSchema = SchemaFactory.createForClass(Review);