0

Can someone explain to me why I am not able to access zvol_name inside subscriber call.

this.rest.get(this.volume_resource_name , {}).subscribe((res) => {
  let zvol_name: Array<String>;
  let zvol_object: Array<object>;
  for(let zvol_list of res.data) {
    zvol_name.push(zvol_list['vol_name']);
  }
  for(let j in zvol_name){
    var result = this.rest.get(this.volume_resource_name+ '/' + zvol_name['vol_name'], {})
    }
});
Vaibhav Chauhan
  • 787
  • 3
  • 11
  • 29

1 Answers1

2

That is because the line let zvol_name: Array<String>; does not actually initialize an array. Your zvol_name variable will still be undefined.

Initialize it with an empty array before using it:

let zvol_name: Array<String> = [];

You can set "strictNullChecks": true in your tsconfig.json file to make sure these errors get caught during compilation.

Saravana
  • 28,153
  • 11
  • 81
  • 94
  • or `let zvol_name: Array = new Array();` See the difference here: http://stackoverflow.com/questions/931872/what-s-the-difference-between-array-and-while-declaring-a-javascript-ar – Zze May 15 '17 at 03:17