0

i have the following code and i'm trying to console log a variable called trt i created below it: how can i get trt in console log?? thanks

          this.rows = this.dataService.SetItem('added-items',());{
      console.log(trt);
      this.dataService.GetJson().subscribe((result)=>
      {
        this.rows=(JSON.parse(result['_body'])); 
        var trt=(result['_body']); 
          //this.rows = this.dataService.SetItem("data",this.rows);

  })

thanks!!

Yossi Bondasd
  • 99
  • 2
  • 11

1 Answers1

0

You need to log it to the console after you define it.

this.rows = this.dataService.SetItem('added-items',());{
  //console.log(trt);
  this.dataService.GetJson().subscribe((result)=>
  {
    this.rows=(JSON.parse(result['_body'])); 
    var trt=(result['_body']); 
     console.log(trt);
      //this.rows = this.dataService.SetItem("data",this.rows);

})

Edit: you cannot call a variable that has not been defined yet and expect it to not be undefined.

Josh Lavely
  • 178
  • 1
  • 2
  • 10
  • i need its value at the above row in order to use it here ('added-items',(trt)); – Yossi Bondasd Oct 16 '17 at 23:10
  • @EladNachum you could get 'trt' and setup a recursive function to loop back when it's gotten that data. – Josh Lavely Oct 16 '17 at 23:11
  • how do i do that?? i need the var trt to get a value and than return to ('added-items',(trt)); so i can put it in localstorage – Yossi Bondasd Oct 16 '17 at 23:13
  • @EladNachum you run the callback inside that function with an if statement to check if you've gotten it. If you have stop execution of that function. – Josh Lavely Oct 16 '17 at 23:15
  • 1
    @EladNachum what you're describing is an impossible thing. You're saying I don't want an apple from the tree but I want am apple before I plant the tree. – Josh Lavely Oct 16 '17 at 23:15