JavaScript variables scope. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

JavaScript variables scope.

the global variable "argument" is getting change in local scope but again returning to original value outside the function scope in console.log(attachments) Firebase/JavaScript code: var attachments="a"; function submitForm(e){ e.preventDefault(); var name = $('#name').val(); var email = $('#email').val(); // Created a Storage Reference with root dir var storageRef = firebase.storage().ref(); // Get the file from DOM var file = document.getElementById("files").files[0]; // Create a reference to the file we want to download var starsRef = storageRef.child(file.name); // Get the download URL starsRef.getDownloadURL().then(function(url) { attachments = url; }).catch(function(error) { // Handle any errors }); console.log(attachments); //OUTPUT "a" saveMessages(name,email,queryType,message,attachments); }

24th Jan 2020, 7:23 PM
harshit
harshit - avatar
3 Answers
+ 2
i see its seems the promise havent resolved. so the new value is not there yet. you can move the part where you need the attachment url inside then. then(function(url){ attarchments = url; console.log(attachments); } or use async await. attachments = await starsRef.getDownloadURL();
24th Jan 2020, 8:28 PM
Taste
Taste - avatar
24th Jan 2020, 7:41 PM
harshit
harshit - avatar
+ 1
Thanks Taste , the former solution worked.
24th Jan 2020, 9:25 PM
harshit
harshit - avatar