首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >快速删除工作但未发现错误

快速删除工作但未发现错误
EN

Stack Overflow用户
提问于 2020-09-21 12:53:49
回答 4查看 357关注 0票数 2

我正在使用样板作为我的api。它使用快捷键和带有打字本的打字机。

当我想要删除一个问题时,它可以正常工作,但是响应是一个404 not found

这是我的Question.ts课程:

代码语言:javascript
复制
@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[];
}

以下是我在控制器中的请求:

代码语言:javascript
复制
@Delete('/:id')
public delete(@Param('id') id: string): Promise<void> {
    return this.questionService.delete(id);
}

下面是我在服务中的方法:

代码语言:javascript
复制
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的请求,以下是控制器的完整文件:

代码语言:javascript
复制
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);
    }
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2021-04-29 12:31:45

我最近有一次入侵。@杰林·D·Joy把我带到了这里,所以谢谢你。样板使用路由控制器。因此,我查看了存储库中的选项类,发现您可以对未定义的返回覆盖默认的响应代码。当然,默认设置是404。我将其更改为200,稍加修改,此功能非常适合我。当前的配置如下所示:

代码语言:javascript
复制
const app: Application = createExpressServer({
  cors: true,
  routePrefix: env.app.routePrefix,
  controllers: env.app.dirs.controllers,
  middlewares: env.app.dirs.middlewares,
  classTransformer: true,
  authorizationChecker: authorizationChecker(connection),
  currentUserChecker: currentUserChecker(connection),
  defaults: {
    undefinedResultCode: 200,
  },
});

这解决了我的问题,因为在每个请求中,当响应未定义时,我都会定义一个自定义错误。我唯一需要做的就是从删除请求中删除@OnUndefined装饰器。

注意:我必须更改一些请求,所以如果采用我的解决方案,请检查哪些请求需要调整。

Note2:解决方案当然也可以通过返回类似于Successfully deleted object之类的字符串来解决,但我更喜欢上面的解决方案,因为我认为这是更好的最佳实践。

票数 0
EN

Stack Overflow用户

发布于 2020-10-07 10:47:08

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

因此,当返回的结果未定义并且没有提到undefinedResultCode时,它将抛出NotFoundError(),从而生成404状态代码。

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

代码语言:javascript
复制
@UndefinedResultCode(200)

删除函数的顶部。

票数 1
EN

Stack Overflow用户

发布于 2020-09-30 10:50:33

好的,您的questionService.delete(id)方法没有返回任何内容,所以delete处理程序正在返回undefined,我猜想Express认为端点未被处理,因此它返回一个404。尝试从questionService中返回一些内容,即:

代码语言:javascript
复制
try {
  await Question.delete(id);
  return 'OK' // add this
} catch (error) {
  this.log.error('Could not delete question: ', id, ' Message: ', error);
}

或强制删除路由处理程序中的空响应(HTTP 204)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63992701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档