0

How to delete an id or a record in database using post request in api

Delete one guide from database

@Delete('/:id')
deleteGuide(@Param('id') id: string): Promise<void> {
    return this.guidesService.deleteGuide(id);
}
5
  • What does your GuidesService look like? What database and or ORM are you using? Commented Nov 5, 2019 at 10:52
  • Are you trying to use a POST request or a DELETE request? The code snippet shows an HTTP DELETE but your question pertains to an HTTP POST, which is causing a bit of confusion. Commented Nov 5, 2019 at 15:02
  • async deleteGuide(id: string): Promise<void>{ const result = await this.guideRepository.delete(id); if(result.affected === 0){ throw new NotFoundException(Guide with ID "$id" not found); } } This is how my GuidesService looks like and also I'm using PostgreSql @KimKern Commented Nov 7, 2019 at 4:13
  • I'm trying to use POST request to delete the record in the database @JayMcDoniel Commented Nov 7, 2019 at 4:14
  • What URL are you trying to hit? You need to change your decorator to a @Post(':id') instead of the DELETE one if you want to use a POST request. Commented Nov 7, 2019 at 4:26

2 Answers 2

1

You can do

@Post('/:id')
deleteGuide(@Param('id') id: string): Promise<void> {
    return this.guidesService.deleteGuide(id);
}
Sign up to request clarification or add additional context in comments.

Comments

0

This works fine too:

@Delete('/:id')
async delete(@Param('id') id: string): Promise<boolean> {
  return this.guidesService.deleteGuide(id);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.