Error on last lines of code

I’m using a custom form - simple form for collecting user input and then submitting to a database and then to send the form data by email to me (plus another)

All seems to be OK in the the web modules (using sendGrid etc) but the .then … at the end of this code is causing error - can anyone help. I’m very new to Java so can’t see a solution easily.

import { sendEmail , sendEmailWithRecipient } from ‘backend/email’ ;

$w . onReady ( function () {
$w ( “#dataset1” ). onAfterSave ( sendFormData );
});

function sendFormData () {
const subject = Type Your Subject Here ${ $w ( "#input1" ). value } ;
const body = Type Here: ${ $w ( "#input1" ). value } \rLabelHere: ${ $w ( "#input11" ). value } \rLabelHere: ${ $w ( "#input11" ). value } \rLabelHere: ${ $w ( "#input11" ). value } \rLabelHere: ${ $w ( "#input11" ). value } \rLabelHere: ${ $w ( "#input11" ). value } \rLabelHere: ${ $w ( "#input11" ). value } \rLabelHere: ${ $w ( "#input11" ). value } ;
const recipient = $w ( “#input11” ). value ;

sendEmailWithRecipient ( subject , body , recipient )
. then ( response => console . log ( response )); (ERROR on this line)

sendEmail ( subject , body )
. then ( response => console . log ( response )); (ERROR on this line)
}

I haven’t changed the subject or body headings nor the labels yet as I wanted to see if this worked before editing etc. I simply copied this code from wix help and checked with similar scenarios on the web.

And this is the error messages associated with those lines …

Hi there :raised_hand_with_fingers_splayed:

I can’t really tell what’s going on since both backend emails are not provided in this post, there is probably an error on at least one of them that returns an error.

Also, the screenshot of the error is not that useful; as we cannot see the whole error object, you should expand them all before taking the screenshot.

Please update the post and we’ll take another look into it.

It seems like it is from the Velo example, have you connect your own keys? Different sendgrid account has different key. You cannot use it without keys

sendGrid Code

//sendGrid.js

import { fetch } from ‘wix-fetch’ ;

export function sendWithService ( key , sender , recipient , subject , body ) {
const url = “xxxxxx//api.sendgrid.xxxx/api/mail.send.json” ;

const headers = {
“Authorization” : "Bearer " + key ,
“Content-Type” : “application/x-www-form-urlencoded”
};

const data = from= ${ sender } &to= ${ recipient } &subject= ${ subject } &text= ${ body } ;

const request = {
“method” : “post” ,
“headers” : headers ,
“body” : data
};

return fetch ( url , request )
. then ( response => response . json ());
}

have removed the https from const url as I cant post this for some reason
and the .com removed too

And this is the page code

$w . onReady ( function () {

});

import { sendEmail , sendEmailWithRecipient } from ‘backend/email’ ;

function sendFormData () {
const subject = Type Your Subject Here ${ $w ( "#input1" ). value } ;
const body = Type Here: ${ $w ( "#input1" ). value } \rLabelHere: ${ $w ( "#input11" ). value } \rLabelHere: ${ $w ( "#input11" ). value } \rLabelHere: ${ $w ( "#input11" ). value } \rLabelHere: ${ $w ( "#input11" ). value } \rLabelHere: ${ $w ( "#input11" ). value } \rLabelHere: ${ $w ( "#input11" ). value } \rLabelHere: ${ $w ( "#input11" ). value } ;
const recipient = $w ( “#input5” ). value ;

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

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

input value11 and 5 and 1 are all valid fields

  1. Put the import line before the:
$w.onReady(function(){

});
  1. I don’t see where you call the sendFormData() function.
  2. You didn’t import the sendWithService to anywhere I can see.
  3. You may like to consider installing the @sendgrid/mail package which is easy to use.

OK thanks - I did change the import line and it didn’t work.

I note your comments however the problem I have is that having seen several videos re Wix script on the subject and having essentially the same input fields on the user form and using the code for the modules and page code (amended where required), it seems to work on the video’s but not when using my form. Not sure I’m understanding where the differences are especially given there are no err msgs showing when running the code. I will continue to research etc.