I've implemented this boilerplate to build my API, utilizing express and typeorm with typescript.
When attempting to delete a question, the deletion process works smoothly but I receive a 404 not found
response.
Below is my Question.ts
class:
@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[];
}
This is the request method in the controller:
@Delete('/:id')
public delete(@Param('id') id: string): Promise<void> {
return this.questionService.delete(id);
}
And here's the corresponding method in the service:
public async delete(id: string): Promise<void> {
this.log.info('Deleting question: ', id);
try {
await Question.delete(id);
} catch (error) {
this.log.error('Failed to delete question: ', id, ' Error message: ', error);
}
}
Despite the successful deletion of the question, a 404 error is being returned. Any insights on why this discrepancy occurs?
Update
Upon request, here is the complete file of the controller:
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);
}
...
// More controller methods can be added here
...
@Delete('/:id')
public delete(@Param('id') id: string): Promise<void> {
return this.questionService.delete(id);
}
}