Unexpacted Token in SendGrid Code

Hi there,

I used SendGrid to send out an email notification to both owner and recipient but for some reason I have an Unexpected Token in the end and I cannot figure out where did I make the mistake. It’s just there in the end. Please help?


});

function sendFormData() {
const subject = Vous avez reçu un formulaire de ${$w("#input1").value} ${$w("#input2").value};
const body = \rNom: ${$w("#input1").value} \rPrénom: ${$w("#input2").value} \rTéléphone: ${$w("#input3").value} \rEmail: ${$w("#input4").value} \rMessage: ${$w("#textBox1").value};

const intrests = “\rIntérêts: “;
if ($w(”#ArticlesCheckbox”).checked) {
intrests = intrests + “\rArticles”;

if ($w("#EtaiementCheckbox").checked) { 
	intrests = intrests + "\rEtaiement"; 
	
if ($w("#PasserelleCheckbox").checked) { 
	intrests = intrests + "\rPasserelle"; 
	
if ($w("#PontCheckbox").checked) { 
	intrests = intrests + "\rPont"; 
	
if ($w("#OutilCoffrantCheckbox").checked) { 
	intrests = intrests + "\rOutil Coffrant"; 
	
if ($w("#ButonCheckbox").checked) { 
	intrests = intrests + "\rButon"; 
	
if ($w("#PortiqueCheckbox").checked) { 
	intrests = intrests + "\rPortique"; 

if ($w("#MaritimeCheckbox").checked) { 
	intrests = intrests + "\rMaritime"; 

if ($w("#DemolitionCheckbox").checked) { 
	intrests = intrests + "\rDemolition"; 

if ($w("#SupportGrueCheckbox").checked) { 
	intrests = intrests + "\rSupport Grue"; 

if ($w("#DemolitionCheckbox").checked) { 
	intrests = intrests + "\rDemolition"; 

if ($w("#AutreCheckbox").checked) { 
	intrests = intrests + "\rAutre";		 

const intrests = “\rRecevoir Newsletter: “;
if ($w(”#NewsletterCheckbox”).checked) {
intrests = intrests + “\rOui”;

const intrests = “\rDonnées Confidentielles: “;
if ($w(”#ConfidentielleCheckbox”).checked) {
intrests = intrests + “\rOui”;

} 

const recipient = $w(“#input4”).value;
sendEmailWithRecipient(subject, body, recipient)
.then(response => console.log(response));

sendEmail(subject, body)
.then(response => console.log(response));

}

Hi Naama,

It seems to me that there are a number of issues with your code:

If you intended on having a series of if statement, then it appears that you’re missing a closing curly bracket on each one. Like this:

if ($w("#ArticlesCheckbox").checked) {
   intrests = intrests + "\rArticles";
}   // this was missing in your code

Also, if you are trying to construct a string from a number of pieces, I believe it should look like this:

const body = 
"\rNom: " + ${$w("#input1").value} +
"\rPrénom: " + ${$w("#input2").value} +
"\rTéléphone: " + ${$w("#input3").value} +
"\rEmail: " + ${$w("#input4").value} +
"\rMessage: " + ${$w("#textBox1").value}; 

I’m not sure how the flagged character at the end fits in due to the problems that I found.

I hope this helps.

Yisrael

1 Like

I would suggest reading the article How to Send an Email on Form Submission .

Hi Yisrael,

How are you? I remember you from the WIX Code course, last week :slight_smile:
Thanks for getting back to me.
Your advice worked ! I was indeed missing the closing curly bracket after each “if”. After adding it the error was removed.
I think My code is up and ready now, but for some reason after submitting the form there is no email coming out… Before I added the if’s for the checkboxes, the emails were going out every time, though ended up in Spam. Now, the data goes in the Database, but no email is sent to both site owner or recipient. In your opinion, would this have to do with SendGrid or something in the code?

Thanks in advance.

Glad to talk to you again :smiling_face:

Hard to say the if statements end up deciding not to send the email or if there is some other reason. Perhaps your getting an error on the email contents.

What do the console.log() statements show in the developers console?

Ah yes, I am still not used to look in there :expressionless: (such a rookie… hehe).
Here’s what I see - I couldn’t really find what it is referring to… any guess? of facts…?

Hi Naama,
Can you please share a link to your site so we can inspect ?
Roi

Hi Roi,

Here you go :slight_smile:
https://tavas-online.wixsite.com/locapal-new

Thanks a lot,
Naama.

Hi Naama,

What page from that site?

Edit: Ah - think I found it. NOUS CONTACTER

After a quick look, it appears that the wrong quote/tick marks are being used.

This (using `):

const subject = `Vous avez reçu un formulaire de ${$w("#Nom").value} ${$w("#Prenom").value}`;

should be this (using '):

const subject = 'Vous avez reçu un formulaire de ${$w("#Nom").value} ${$w("#Prenom").value}';

Fixing all instances of ` to ’ should greatly help.

Yisrael

Thanks Yisrael,

I tried it, and actually it “errored” the entire sequence.


To be honest, the part you were referring to (above) was there before I added the Boolean dedicated section and the emails were sent out fine, with these tick marks ( ` ). So I don’t think that’s the source of my problem :expressionless: … any other guess?.. I really think it has to do with the boolean part, something there is for sure not written the right way to be able to “speak” with sendgrid properly.

I would suggest this sequence:

  const body = 
    '\rNom:' + $w("#Nom").value + 
    '\rPrénom:' + $w("#Prenom").value +
    '\rTéléphone:' + $w("#Telephone").value +
    '\rEmail:' + $w("#Email").value +
    '\rMessage:' + $w("#Message").value;

OK… so that worked. No errors there, but the consul is still showing this.
What is this mysterious TypeError : n


And for this sequence there’s an error (see image bellow):

  const recipient = $w("#Email").value;
  	sendEmailWithRecipient(subject, body, recipient)
    .then(response => console.log(response));
    
  sendEmail(subject, body)
    .then(response => console.log(response));
}

Good morning Naama,

I hope you don’t mine, but I fixed (and saved) the code on your page. I kept a copy of the original code just in case you prefer that code instead. :wink:

A few things:

  • You had two onReady() functions. That’s a no-no.

  • const doesn’t work on variables that change, so I changed to let

  • I changed another line of code to change ` to
    I tested it on a cloned version of your site and I actually got an email from it. I did get an error message regarding an error on the dataset saving some fields. You’ll have to investigate that.

I hope this helps.

Yisrael

Wow, Yisrael. I really do appreciate it…
I don’t mind you took the initiative to fix this and no reason for me to save a code that is not functioning.
I will now take a look to learn and understand what I have done wrong, and of course test it to see if finally I can use my first hand-made form :slight_smile:

Will let you know if it goes smooth or not.

Naama,.

1 Like

Hi again,

So… I tested the form and I want to cry :frowning: The email did get sent out to both parties, but it is not showing the info correctly.
This is how it looked like before adding the Boolean options into the code:


It was nice and neat, though still ended up in Spam…

And this is how it looks now:


The data is all in one line and unfortunately, still not showing the Boolean section.

What can I do now?

(BTW - the data you’v submitted is not showing in the database…)

No sure why the from changed format. My changes shouldn’t have affected that. Strange.

Can’t understand why it’s not formatted properly. I’ll look into it.

As I mentioned in my previous post, I did get a dataset error. Perhaps the fields were disconnected somewhow?

Edit: btw - the data gets saved in my cloned site.

Hey Naama,

Well, some data got saved. I found this gem in the message field of your database :smiling_face:
" This is a test after dear Yisrael fixed the code for me… "

So, something is being saved, and as I mentioned in the edit of my previous post, I see that data is being saved in my cloned site as well.

Right now it’s the formatting puzzle that is vexing.

Got it! Wasn’t too bad. My fault!

In my zeal to uncover the problem, I got rid of the critical ** marks. These enclose a template literal, and therefore I need to restore parts of your code - that is, I need to change **'** to ** .

Close the editor so I can go and fix things without causing a conflict with your actions.

As I mentioned long-long ago in the beginning of this thread - the submitted data was always saving properly in the Database, including marked Boolean checkboxes. The problem is that it isn’t registering in the notification emails.
With all your efforts, and regardless of how pretty the email should be, unfortunately I still don’t see the checked Booleans registered in the notification email… Did you get the checked booleans showing on your test notification email?