-1

var image is not defined in alert :( please help , thank you so much!

handleBeforeUpload (file, event) {
   var reader = new FileReader();
   reader.readAsDataURL(file);
   reader.onload = (function () {
     var f = reader.result;
   }); 

   var image = f;
   alert(image)

   var photo = {uri: image}
Mario Nikolaus
  • 2,346
  • 1
  • 21
  • 28
user3006575
  • 147
  • 1
  • 1
  • 12
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Mika Sundland Feb 04 '18 at 17:31

1 Answers1

1

Your f variable is in scope of onload function callback. Define it outside of that function, where you define your reader variable, so it will be available in scope of handleBeforeUpload function

handleBeforeUpload (file, event) {
   var reader = new FileReader();
   var photo = null;
   reader.readAsDataURL(file);
   reader.onload = (function () {
     f = reader.result;
     photo = { uri: f }
   }); 
}
Mario Nikolaus
  • 2,346
  • 1
  • 21
  • 28
  • response null :( – user3006575 Feb 04 '18 at 16:27
  • if after f=reader.result try get console.log , data show, but alert have null ( – user3006575 Feb 04 '18 at 16:28
  • i think need use promise ? – user3006575 Feb 04 '18 at 16:28
  • Yes! You are calling async function and your alert is called before onload callback is called. Check updated answer, I added console.log in the callback itself so you can inspect the actual file object in console – Mario Nikolaus Feb 04 '18 at 16:31
  • console.log(f); return data, but i'm not understand how save this data if you can please build correct version , thank you!.] – user3006575 Feb 04 '18 at 16:33
  • May be out of context , but i just play little with it, may be worth watching , https://jsfiddle.net/parlad/un04n1dg/ , This is complete structure of file part. thank you – parlad Feb 04 '18 at 16:34
  • @MarioNikolaus its full code, save in global var f = ''' now response null in alert – user3006575 Feb 04 '18 at 16:42
  • After reader.onload = (function () { f = reader.result; console.log(f); <- have data }); alert(f) -> no data – user3006575 Feb 04 '18 at 16:44
  • It will be null, I left it there because it is referenced in question. What does your console log say? – Mario Nikolaus Feb 04 '18 at 16:45
  • @MarioNikolaus console.log(f); response base64 code, how this data send to alert(image) ? – user3006575 Feb 04 '18 at 16:47
  • So you have your image in that callback, forget about alert that code is wrong, I updated my answer. Do whatever you need to do with image in that callback – Mario Nikolaus Feb 04 '18 at 16:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164499/discussion-between-user3006575-and-mario-nikolaus). – user3006575 Feb 04 '18 at 16:57
  • its not good for me , becouse photo will stay null need return data after onload maybe must use promise function , after suss return and write data in var photo . Maybe i'm mistake – user3006575 Feb 04 '18 at 17:04