Impossible to attach file to an email using SendGrid

Hello,

I know this question has been asked thousand of times but I follow every tutorial on the site, I tried everything and it still doesn’t work.

I can received the mail which is great. But I can’t get the file uploaded by the customer. On the mail I get I only saw undefined.

I tried adding to my code the “match” code but i get this error when I try the form :

UserError: An error occurred in one of afterSave callbacks Caused by TypeError: Cannot read properties of undefined (reading 'match')

So what am I doing wrong ?
my page got a DB called : #DataCandidature.
I have some input field (text and date) and an upload field (called #cv)
here a pic of how the website looks like (sorry it’s in French):

This is the front code :

import { sendCVToUser, sendCVToAdmin } from 'backend/sendEmail';

$w.onReady(function () {
    $w("#DataCandidature").onAfterSave(sendFormData);
});

function sendFormData() {
    const convertRegex = new RegExp(/wix:document:\/\/v1\/([^\/]+)\/(.*)$/);
    const item = $w("#DataCandidature").getCurrentItem();
    const matches = item.cv.match(convertRegex);
    const cvurl = `docs.wixstatic.com/ugd/${matches[1]}?dn=${matches[2]}`;
    const contentfile = $w("#cv").value;
    const subjectAdmin = `Candidature ${$w("#input7").value} - ${$w("#UserLastName").value} ${$w("#UserFirstName").value}`;
    const subjectRecipient = `${$w("#UserFirstName").value} Candidature reçue`;
    const bodyRecipient = `Bonjour ${$w("#UserFirstName").value},
        \rNous accusons la bonne reception de votre candidature sur le poste de ${$w("#input7").value}
        \rVotre CV a été transmis auprès de l'employeur qui ne manquera pas de revenir vers vous si votre profile lui convient.`;
    const bodyAdmin = `Bonjour,
        \r${$w("#UserFirstName").value} vient de candidater sur le poste de ${$w("#input7").value} sur la ville de ${$w("#input8").value}.
        \rVoici les informations du jeune :
        \rPrénom : ${$w("#UserFirstName").value}
        \rNom de famille : ${$w("#UserLastName").value}
        \rVille du Jeune : ${$w("#UserVille").value}
        \rDate de Naissance du Jeune : ${$w("#UserBirth").value}
        \rNuméro de téléphone : ${$w("#UserPhone").value}
        \rAdresse email : ${$w("#UserMail").value}
        \rCV du jeune : ${cvurl}
        \rCV : ${contentfile}
        \rMerci de transmettre le CV auprès de l'employeur si vous êtes la personne en charge de l'offre.`;
    const UserRecipient = $w("#UserMail").value;
    const AdminRecipient1 = "my_email_adress"
    sendCVToAdmin(subjectAdmin, bodyAdmin, AdminRecipient1)
        .then(() => sendCVToUser(subjectRecipient, bodyRecipient, UserRecipient));
}

Again, if I remove the following code :

const convertRegex = new RegExp(/wix:document:\/\/v1\/([^\/]+)\/(.*)$/);
const item = $w("#DataCandidature").getCurrentItem();
const matches = item.cv.match(convertRegex);
const cvurl = `docs.wixstatic.com/ugd/${matches[1]}?dn=${matches[2]}`; 

I’m able to send my email with no error and I received it but with no file or url to download my file.

Any idea what I’m doing wrong?

Regards
Jorys

Try:

$w.onReady(function () {
	$w("#DataCandidature").onReady(() => {
    		$w("#DataCandidature").onAfterSave(sendFormData);
	})
});

Also add console.log(item) after the item declaration and see what it logs and make sure it has the “cv” field + (check that the cv value type is “string”),

Hi, I modify the code with the one you gave me.

I add console.log(item) after the item.

I get the same error has before.

So I delete the CV field in my database and made a new one and it worked now… Well I guess I mistake somewhere but thank you for your help :slight_smile:

If you were able to create a new cv field it means the previous field didn’t have a “cv” field key at all (otherwise it wouldn’t let you recreate the same field even after you deleted the previous one).
Maybe the field label of the previous one was “cv” but the key was something else.
Anyway, I’m glad it works for you.