alexis
alexis

Reputation: 3

Angular 8 Subscribe not getting the return data from service

I want to Patch the data to my forms after passing the ID to the service and getting the data on my SERVER but I don't get any data. here is my code

addForm: FormGroup;
  ngOnInit(){
    const routesParams=this.routes.snapshot.params;


    this.addForm=this.formBuilder.group({
      Title:['',[Validators.required,Validators.minLength(1)]],
      Body:['',[Validators.required,Validators.minLength(1)]],
      Author:['',[Validators.required,Validators.minLength(1)]],
    })
     console.log(routesParams.post_id);

    this.blogService.getBlogbypost_id(routesParams.post_id).subscribe((data:any)=>{
      this.addForm.patchValue(data);

    });
  }

code for my service.ts

getBlogbypost_id(id:number){
  return this.http.get<cBlog[]>(this.updateUrl+id);
}

I also get the data on the server but not fetching on subscribe((data: any))

Upvotes: 0

Views: 727

Answers (2)

Ilija Iličić
Ilija Iličić

Reputation: 507

Try to patch value on this way - I don't know how your data structure looks like and then I wrote this on my way but you'll get it - take a look:

this.blogService.getBlogbypost_id(routesParams.post_id).subscribe((data:any)=>{
    this.addForm.patchValue({
        Title: data.title,
        Body: data.body,
        Author: data.author
    });
});

patchValue doesn't really catch nesting errors and you cannot know what is going on sometimes as the documentation says https://angular.io/guide/reactive-forms.

Hope that will help!

Upvotes: 1

Sunny Parekh
Sunny Parekh

Reputation: 983

Hi change the code as mentioned below:

    getBlogbypost_id(id:number): Observable<cBlog[]>{
      return this.http.get<cBlog[]>(this.updateUrl+id);
    }



 this.blogService.getBlogbypost_id(routesParams.post_id)().subscribe((data:any)=>{
      console.log(data);
      this.addForm.patchValue(data);
    });

Let me know what is the outcome!

Upvotes: 0

Related Questions