javascript - ERROR TypeError: Cannot set property 'abc' of undefined Angular2 -


i have input type file html element , want upload , read json file , store file's object content in local variable. suppose json object getting after reading file :-

{    name:{         firstname: "name1",         lastname: "name2"    } } 

now, want store object in variable after reading file,

private jsondata = {};  readfile(eve){     let reader:any,     target:eventtarget;     reader= new filereader();      reader.onload = function(eve:any) {         this.jsondata['json_def'] = json.parse(eve.target.result);          console.log(this.json_def);     }     reader.readastext(eve.target.files[0]); } 

this error receiving in console when upload it:

error typeerror: cannot set property 'json_def' of undefined @ filereader.reader.onload (create-model.component.ts:135) @ zonedelegate.invoke (zone.js:391) @ object.oninvoke (core.es5.js:3890) @ zonedelegate.invoke (zone.js:390) @ zone.runguarded (zone.js:154) @ filereader.<anonymous> (zone.js:132) 

and not getting exact reason of error. please help. thanks.

this in reader.onload not object's context. haven't variable name jsondata. can use arrow function preserve context object.

reader.onload = (eve:any) => {     this.jsondata['json_def'] = json.parse(eve.target.result);      console.log(this.json_def); } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -