首页 > 解决方案 > 快速删除工作但未找到错误

问题描述

我将这个样板用于我的 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);
    }
}

标签: typescriptexpresstypeorm

解决方案


从您发布的错误日志中,我发现这个错误是从 ExpressDriver.ts 文件中抛出的。所以,在检查来自 github 的文件时,我看到了这个逻辑。

在此处输入图像描述

因此,当返回结果为 undefined 且未提及 undefinedResultCode 时,将抛出 NotFoundError() 导致 404 状态码。

尝试从中返回一些响应,看看问题是否仍然存在或添加

@UndefinedResultCode(200)

到删除功能的顶部。


推荐阅读