0

Is there any way to fetch data (by id) at obj obj ?

example:

  hello(){
    this._shelveService.getShelveDataById(this.route.snapshot.params.id).subscribe(data => {
      console.log(data);

  this._shelveForm = this.fb.group({
    id_shelve:[data['id_shelve']],
    description_shelve:[data['description_shelve']],
    war_id:this.fb.group({
      id_warehouse: [data['war_id.id_warehouse']]
    })
 })
    })
  }

how to get the value of id_warehouse ?

I can get the value id_shelve and description_shelve with the hello() function but if I type:

war_id:this.fb.group({
  id_warehouse: [data['war_id']] 

i get as a result Obj Ojb

json :

{
    "id_shelve": 1,
    "description_shelve": "Περιγράφη Ραφίου!!!",
    "war_id": {
        "id_warehouse": 1,
        "description_warehouse": "Περιγράφη Αποθήκης!!!"
    }
}

Any help;

2 Answers2

0

You can solve this by either of the two approaches below:

a) Dot notation:

this._shelveForm = this.fb.group({
    id_shelve: [data['id_shelve']],
    description_shelve: [data['description_shelve']],
    war_id: this.fb.group({
        id_warehouse: [data.war_id.id_warehouse],
        description_warehouse: [data.war_id.description_warehouse]
    })
});

b) Square bracket notation:

this._shelveForm = this.fb.group({
    id_shelve: [data['id_shelve']],
    description_shelve: [data['description_shelve']],
    war_id: this.fb.group({
        id_warehouse: [data['war_id']['id_warehouse']],
        description_warehouse: [data['war_id']['description_warehouse']]
    })
});
Mihai Paraschivescu
  • 1,261
  • 13
  • 12
  • you are a truly legend i was dealing with this issue about 5 hours,I'm so noob :( – Amateur Developer Aug 28 '21 at 15:05
  • Being a newbie/noob is not a bad thing, as long as you're a constant learner! Btw, there are certain situations in which what you tried to do initially might have sense. For eg if one of the fields inside the JSON data contains a dot in its name. For more details over this, you can refer to this answer: https://stackoverflow.com/a/4968448/5780536 – Mihai Paraschivescu Aug 28 '21 at 15:09
0

Replace:

war_id:this.fb.group({
  id_warehouse: [data['war_id']] 

with

war_id:this.fb.group({
  id_warehouse: [data['war_id']['id_warehouse']] 
Ritesh Waghela
  • 3,474
  • 2
  • 19
  • 25