0

I am working on this project and I got stuck here: When I click on the button the image that was uploaded earlier should show. It shows, but only if I click on the button 2-3 times. It looks like it takes some time before i can use the storage ref.

`$(document).on("click",".edit", function(){
    var id = $(this).data('edit');
  firebase.storage().ref('Images/'+id).getDownloadURL().then(function(url){
    imgurl=url;
    
  });

  $(".myimg").attr("src", imgurl);

`

  • It's not clear what your issue is. But, perhaps you are clicking faster than the response completes. Also, I'd recommend not using jQuery. – mwilson Mar 04 '21 at 23:13
  • `imgurl` is only defined after the firebase call completes, which is long after `$(".myimg").attr("src", imgurl);` occurs. simply move the .attr call into the callback where you defined `imgurl` – Kevin B Mar 04 '21 at 23:19
  • The callback passed to `then` method is executed asynchronously. More specifically undefined is set as image src attribute first and the assignment inside calback `imgurl=url` is executed later. You forgot to declare imgurl variable (missing var, let or const key word) thus it has global scope. That is why src attribute is set in the second click handler call with the value asssign to imgurl in the first call. – Arek Kostrzeba Mar 04 '21 at 23:19

0 Answers0