Taking data from .then

I am trying to send a form with an upload button and text fields. My problem is that I don’t think I can send emails via sendgrid, so I was thinking of just sending the url of the uploaded image.

Here’s my code at the moment…

import {sendEmail} from ‘backend/email’;
import wixLocation from ‘wix-location’;

export function submitButton_click(event) {
sendFormData();
}

function upfile() {
if($w(“#IconOrElement”).value.length > 0) {
$w(“#IconOrElement”).startUpload()
.then( (uploadedFile) => {
let uploadfileurl = uploadedFile.url;

	      	console.log("test in upfile: " + uploadedFile.url); 
	        let upString = uploadfileurl.split("/"); 
	      	console.log(upString[3]); 
			 
	         return upString; 
 		  }); //End of then	 		   
  } 

}

function sendFormData() {

  let uf = upfile(); 
  console.log("test 3 outside upfile: " + uf); 

  var subject = "Logo Discovery Form Submission: "; 
  var body = `Company or Product: ${$w("#CompanyOrProduct").value} 
    \rIconOrElement: ` + uf +  
    `\r\rCompetitor Website: ${$w("#CompetitorWebsite").value} 
	\rAdditional Information: ${$w("#AdditionalInformation").value}`; 

  sendEmail(subject, body);     

return true;
}

So yeah, it keeps just returning an undefined value for “upString”, when I want it to be the url to the uploaded image. I tried putting the var subject, var body and sendEmail(subject,body); in the .then, but it then doesn’t show the ${$w(“#CompetitorWebsite”).value or ${$w(“#AdditionalInformation”).value

How do you retrieve the uploadFile.url from a .then?

Hi,

There’s a main thing about promises and asynchronous code to wrap your head around…
Here you have a function:

(uploadedFile) => {
		      	let uploadfileurl = uploadedFile.url;
		      	
		      	console.log("test in upfile: " + uploadedFile.url);
		        let upString = uploadfileurl.split("/");
 		      	console.log(upString[3]);
				 
		         return upString;
  	 		  }); //End of then

This function is asynchronous, meaning i runs in an unknown time… Therefore, using a ‘return’ statement is meaningless because no one really ‘called the function’, as opposed to:

let result = someFuntion();

Where the value of ‘somFunction()’ is returned synchronously.

So to deal with your issue, instead of ‘return’ just call ‘sendFormData’ with the url:

(uploadedFile) => {
		      	let uploadfileurl = uploadedFile.url;
		      	
		      	console.log("test in upfile: " + uploadedFile.url);
		        let upString = uploadfileurl.split("/");
 		      	sendFormData(upString);
  	 		  }); //End of then
function sendFormData(upString) {
	
      let uf = upString;
      //The rest of the function here...

Hope this helps,

Liran.

Thank you, yeah I have a lot to learn. This helps a bunch though! :slight_smile: