0

How to fetch data from the database and set the data from the database into an input box

Following is a piece of my code

get service

getStudentAddress() {
 const id = sessionStorage.getItem('userId');
 return this.http.get('http://localhost:8080' + '/student/' + id);
}

The getStudentAddress() that will return an array of the student data something like this

An Array of student data from backend

in my component, how can I get the data and put it on an input box that will display the data automatically when the page is open

ngOnInit() {

 // Here should the display is from the database by using setValue or patchValue
 this.studentForm.setValue({
  s_pNumber: '1234',
  s_address: '123',
  s_address2: '123',
  s_pCode: '123',
  s_city: '123',
  s_state: '123',
  s_country: '123'
});
Dory
  • 95
  • 4
  • 19
  • Hi, may I know what are the names of the FormControls in your FormGroup? – wentjun Apr 05 '19 at 02:43
  • from html right? if that so, one of my input formControlName is s_pNumber – Dory Apr 05 '19 at 02:46
  • Yes, I am asking that because I want to know if all of the properties in your response body are the same as your formControlNames. Anyways, I have updated my answer to use `this.studentForm.patchValue(response.data);` instead of `this.studentForm.patchValue(response);` – wentjun Apr 05 '19 at 02:50
  • yeap, its the same. – Dory Apr 05 '19 at 02:51

1 Answers1

4

You should return the observable value by subscribing to the API call, then use patchValue to assign the, to your reactive form. I am assuming that your FormControl names are the same as the properties in that object. On your component.ts,

constructor(private crudService: crudService) { }

ngOnInit() {
  this.crudService.getStudentAddress().subscribe((response: any) => {
    this.studentForm.patchValue(response.data);
  });   
}

For more details on setting values to your FormControl, you may refer to this other answer of mine.

wentjun
  • 40,384
  • 10
  • 95
  • 107
  • Its said Property 'data' does not exist on type 'Object'. – Dory Apr 05 '19 at 02:51
  • I have tried the (response) only but nothing happen – Dory Apr 05 '19 at 02:52
  • What is the name of your service class? Also, the GET request is working, right? – wentjun Apr 05 '19 at 02:54
  • my service class is crudService and the GET via postman is working. And when i run the page, https://imgur.com/a/NJ5Yjjr this is what i get. Its working when taking the data from the db but it did not display on the input box – Dory Apr 05 '19 at 02:58
  • Alright, did you import `crudService` into your component? – wentjun Apr 05 '19 at 03:00
  • Ok, ermm, if you add `console.log(response);` before `this.studentForm.patchValue(response.data);`, does your console print anything? – wentjun Apr 05 '19 at 03:06
  • it will display an array of the data same like this imgur.com/a/NJ5Yjjr . Is it because the array got name? and thats why its not working? Because i put a name of the array called data as you can see on the array – Dory Apr 05 '19 at 03:10
  • Hehe.. Sama sama :) – wentjun Apr 05 '19 at 03:15
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191289/discussion-between-wentjun-and-hisyam-syazani). – wentjun Apr 05 '19 at 03:25
  • 1
    I have edited your code to remove the error :) Thanks again for helping – Dory Apr 05 '19 at 03:28