typescript - 快速删除工作但未找到错误
问题描述
我将这个样板用于我的 api。它使用带有打字稿的 express 和 typeorm。
当我想删除一个问题时,它可以正常工作,但响应是404 not found
.
这是我的Question.ts
课:
@Entity()
export class Question extends BaseEntity {
@PrimaryColumn('uuid')
public id: string;
@IsNotEmpty()
@Column()
public title: string;
@IsNotEmpty()
@Column({
length: 2000,
})
public description: string;
@IsNotEmpty()
@Column()
public answered: boolean;
@ManyToOne(type => User, user => user.questions, { onDelete: 'SET NULL', onUpdate: 'CASCADE' })
public user: User;
@IsNotEmpty()
@ManyToMany(type => Tag)
@JoinTable()
public tags: Tag[];
@OneToMany(type => Answer, answer => answer.question)
public answers: Answer[];
@OneToMany(type => Comment, comment => comment.question)
public comments: Comment[];
}
这是我在控制器中的请求:
@Delete('/:id')
public delete(@Param('id') id: string): Promise<void> {
return this.questionService.delete(id);
}
这是我在服务中的方法:
public async delete(id: string): Promise<void> {
this.log.info('Delete questions: ', id);
try {
await Question.delete(id);
} catch (error) {
this.log.error('Could not delete question: ', id, ' Message: ', error);
}
}
当我在删除后得到所有问题时,问题不再存在,因此问题已成功删除。为什么我得到一个 404 虽然我的删除工作?
更新
根据 aRvi 的要求,这里是控制器的完整文件:
import { Request } from 'express';
import {
Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam, Req
} from 'routing-controllers';
import { ResponseSchema } from 'routing-controllers-openapi';
import { QuestionNotFoundError } from '../errors/QuestionNotFoundError';
import { Answer, Comment, PagedResult, Question, Tag, User } from '../models/Models';
import { QuestionService } from '../services/Services';
import { CreateQuestionBody } from './Bodies/CreateQuestionBody';
import { PutQuestionBody } from './Bodies/PutQuestionBody';
import { QuestionResponse } from './responses/Responses';
@JsonController('/questions')
export class QuestionController {
constructor(
private questionService: QuestionService
) { }
@Get()
// tslint:disable-next-line:max-line-length
public async find(@Req() req: Request, @QueryParam('skip') skip: number, @QueryParam('take') take: number, @QueryParam('orderBy') orderBy: string, @QueryParam('where') where: string): Promise<PagedResult<Question>> {
const questions = await this.questionService.find();
return new PagedResult<Question>().Create(req, questions, skip, take, orderBy, where);
}
@Get('/:id')
@ResponseSchema(QuestionResponse)
@OnUndefined(QuestionNotFoundError)
public findOne(@Param('id') id: string): Promise<Question | undefined> {
return this.questionService.findOne(id);
}
@Get('/:id/answers')
@OnUndefined(QuestionNotFoundError)
// tslint:disable-next-line:max-line-length
public async findAnswers(@Req() req: Request, @Param('id') id: string, @QueryParam('skip') skip: number, @QueryParam('take') take: number, @QueryParam('orderBy') orderBy: string, @QueryParam('where') where: string): Promise<PagedResult<Answer> | undefined> {
const answers = await this.questionService.findAnswers(id);
return new PagedResult<Answer>().Create(req, answers, skip, take, orderBy, where);
}
@Get('/:id/comments')
@OnUndefined(QuestionNotFoundError)
// tslint:disable-next-line:max-line-length
public async findComments(@Req() req: Request, @Param('id') id: string, @QueryParam('skip') skip: number, @QueryParam('take') take: number, @QueryParam('orderBy') orderBy: string, @QueryParam('where') where: string): Promise<PagedResult<Comment> | undefined> {
const comments = await this.questionService.findComments(id);
return new PagedResult<Comment>().Create(req, comments, skip, take, orderBy, where);
}
@Get('/:id/tags')
@OnUndefined(QuestionNotFoundError)
// tslint:disable-next-line:max-line-length
public async findTags(@Req() req: Request, @Param('id') id: string, @QueryParam('skip') skip: number, @QueryParam('take') take: number, @QueryParam('orderBy') orderBy: string, @QueryParam('where') where: string): Promise<PagedResult<Tag> | undefined> {
const tags = await this.questionService.findTags(id);
return new PagedResult<Tag>().Create(req, tags, skip, take, orderBy, where);
}
@Post()
@ResponseSchema(QuestionResponse)
public async create(@Body() body: CreateQuestionBody): Promise<Question | undefined> {
const question = new Question();
question.title = body.title;
question.description = body.description;
question.tags = body.tags;
// TODO: change to current user
const users = await User.find();
const user = users[0];
question.user = user;
return this.questionService.create(question);
}
@Put()
@ResponseSchema(QuestionResponse)
public updateMerge(@Body() body: PutQuestionBody): Promise<Question | undefined> {
return this.questionService.updateMerge(body);
}
@Post('/:id')
@ResponseSchema(QuestionResponse)
public updateOne(@Param('id') id: string, @Body() body: CreateQuestionBody): Promise<Question | undefined> {
const question = new Question();
question.title = body.title;
question.description = body.description;
question.tags = body.tags;
return this.questionService.updateFull(id, question);
}
@Delete('/:id')
public delete(@Param('id') id: string): Promise<void> {
return this.questionService.delete(id);
}
}
解决方案
推荐阅读
- mysql - 嗨我如何锻炼这个mysql查询问题
- python-3.x - 如何获得networkx中无向边的权重?
- parsing - 条件运算符是如何解析的?
- flutter - Flutter Firestore Streambuilder 与未来
- json - Flutter json.decode 问题错误
- sql - 带有 CASE 条件的 SQL 可以在 PostgreSQL 中使用索引吗?
- swift4 - 在表格视图单元格中按首字母 Gold 的字母顺序对快速射击基础中的多个数据进行排序
- python-3.x - 将数据帧分数转换为浮点数
- java - 在 Spring Boot 中测试 javax.imageIo 找不到阅读器
- python - python 输出中的 Power shell 命令给出整数。为什么?