3

I'm trying to delete an article using http.delete but Angular isn't making the request. I'm not sure what's missing.

blog-service.ts:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/map';
const BASE_URL = 'http://localhost:8080';

[...]

deleteArticle(id) {
    this.http.delete(`${BASE_URL}`+'/api/articles/'+ id)
}

edit-blog.component.ts:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from "@angular/router";
import { BlogService } from "../../../services/blog.service";

[...]

onPostDelete() {
    this.blogService.deleteArticle(this.urlParam.id)
}

ngOnInit() {
    this.urlParam = this.activatedRoute.snapshot.params;

    this.blogService.getArticle(this.urlParam.id)
    .subscribe(data =>this.article = data);
}

edit-blog.component.html:

<form [formGroup]="createPostForm" (submit)="onPostSubmit()" autocomplete="off">
[...]
</form>

<button class="btn btn-outline-danger" (click)="onPostDelete()">Delete Post</button>

Could somebody tell me what's missing? thanks

2
  • 1
    You need to subscribe to the Observable returned from delete. Commented Jan 10, 2018 at 17:45
  • I tried that as suggested here: stackoverflow.com/questions/46603088/…, but it throws the error "Cannot read property 'subscribe' of undefined" Commented Jan 10, 2018 at 17:56

1 Answer 1

6

Inorder to make the request, you need to subscribe as follows

onPostDelete() {
   this.blogService.deleteArticle(this.urlParam.id).subscribe((response) => {
     console.log("deleted"));
   });
}

and modify your blog-services as follows,

deleteArticle(id) {
   return this.http.delete(`${BASE_URL}`+'/api/articles/'+ id).map((response: Response) => response.json())
}
Sign up to request clarification or add additional context in comments.

3 Comments

@KirkLarkin yep i did not see that
Oh, return! Thanks
@Sajeetharan What if I need to send requestBody in delete request?

Your Answer

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