我正在使用nestjs和mongoose制作一个API。但是,我被一个bug卡住了。我向其他人寻求帮助,并且尝试了googe,但没有成功。简而言之,我的问题是,当向API端点发出POST请求时,它只在mongoose中保存teh_id和__v。此外,它只在这个特定的端点上这样做。
下面是相关代码:
application.schema.ts
import { Prop, SchemaFactory } from "@nestjs/mongoose";
import mongoose, { Document } from 'mongoose';
export type ApplicationDocument = Application & Document
export class Application {
@Prop({ required: true, unique: true, type: mongoose.Schema.Types.ObjectId })
id: string;
@Prop({ required: true })
event: string;
@Prop({ required: true })
user: string;
@Prop({ required: true})
start: string;
@Prop({ required: true})
end: string;
@Prop({ required: true})
positionsRequested: string[];
@Prop({ required: true})
facilitiesRequested: string[];
@Prop({ required: true})
notes: string;
}
export const ApplicationSchema = SchemaFactory.createForClass(Application);
createApplication.dto.ts
import { ArrayNotEmpty, IsArray, IsNotEmpty, IsString } from "class-validator";
export class CreateApplicationDto {
@IsString()
@IsNotEmpty()
event: string;
@IsString()
@IsNotEmpty()
user: string;
@IsString()
@IsNotEmpty()
start: string;
@IsString()
@IsNotEmpty()
end: string;
@IsString({ each: true })
@IsArray()
@ArrayNotEmpty()
positionsRequested: string[];
@IsString({ each: true })
@IsArray()
@ArrayNotEmpty()
facilitiesRequested: string[];
@IsString()
@IsNotEmpty()
notes: string;
}
events.controller.ts
@Post('applications')
postApplications(@Body() dto: CreateApplicationDto) {
return this.eventService.postApplications(dto)
}
events.module.ts
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { EventsController } from './events.controller';
import { EventsService } from './events.service';
import { ApplicationSchema } from './schemas/application.schema';
import { EventsSchema } from './schemas/event.schema';
import { AllocationSchema } from './schemas/allocation.schema';
@Module({
imports: [
MongooseModule.forFeature([
{ name: 'events', schema: EventsSchema },
{ name: 'applications', schema: ApplicationSchema },
{ name: 'allocations', schema: AllocationSchema }
])
],
controllers: [
EventsController
],
providers: [
EventsService
],
exports: [
EventsService
]
})
export class EventsModule {}
events.service.ts
constructor(
@InjectModel('events')
private eventsModel: Model<EventDocument>,
@InjectModel('applications')
private applicationsModel: Model<ApplicationDocument>,
@InjectModel('allocations')
private allocationsModel: Model<AllocationDocument>
) {}
// ... irrelevant code ...
async postApplications(dto: CreateApplicationDto): Promise<Application> {
try {
// Check for duplicate
let dup: Array<object> = await this.applicationsModel.find({
dto
})
// If duplicate found, not allowed
if (dup.length > 0) throw new ForbiddenException("Application Already Submitted")
// Get event
let event = await this.eventsModel.findOne({ sku: dto.event })
// If no event, return 404
if (!event) throw new NotFoundException()
let eventStart = event.start
let eventEnd = event.end
if (
// Start before end
!(new Date(dto.start).getTime() < new Date(dto.end).getTime()) ||
// Start and end during event
!(new Date(dto.start).getTime() >= new Date(eventStart).getTime() &&
new Date(dto.end).getTime() <= new Date(eventEnd).getTime())
) throw new ForbiddenException("Incorrect start or end")
const app = new this.applicationsModel(dto);
return app.save()
} catch (err) {
throw err
}
}
mongo
中保存的内容
谢谢你的帮助!
2条答案
按热度按时间c7rzv4ha1#
也许您需要在application.schema.ts文件中的Application类之上包含Schema装饰器。
8aqjt8rx2#
我写了一段代码,然后推到github。在我的例子中,我得到了一个错误。所以我修复了它,并且运行良好。我使用我的mongodb Atlas来模拟你的代码
我只添加了“应用程序标识符=应用程序标识符”(/src/events/events.service.ts 59)部分。
下面是结果。x1c 0d1x
您可以查看github链接。https://github.com/Alex-Choi0/stackoverflow_problams1