0

I am trying to send data to a node js server and then send a message to my email address but I have failed to figure out how to fix this error

TIS IS THE FRONTEND CODE

let donationForm = document.getElementById('donation-form');
const donorUrl = 'https://friendsofbatwaintl.org/donordetails/';

async function  donorForm(event){
    event.preventDefault()
    console.log("Donor sent details")
    const sendEmail = await fetch(donorUrl, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        },
        body: JSON.stringify({
            "donorName": donorName.value,// input value  that is to be submitted  to the node js server application 
        });
    });
    console.log(sendEmail)
    const bodyResponse = await sendEmail.json();
    console.log(bodyResponse)
}
donationForm.addEventListener('submit', donorForm)

the above code is supposed to submit form data input by the user to the server applapplication called donordetails
THIS IS MY BACKEND CODE

async function serverRequest() {
    const sendEmail ={
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
          'Authorization': 'Bearer '
        },
        body: JSON.stringify({
            "donorName": donorName.value,// input valuee    
        }); // body data type must match "Content-Type" header
  
    } console.error(sendEmail);
    const bodyResponse = await sendEmail.json();
    console.error(bodyResponse);
    return bodyResponse
}
http.createServer(function (req, res) {
    let data = '';
    let reqBody = {}
    req.on('data', chunk =\> {
      data = data + chunk;
    });
    req.on('end', () =\> {
      console.log(data)
      reqBody = JSON.parse(data);
      let donorName = reqBody.donorName
      // desc, email, phone
        serverRequest().then(async function () {
          res.writeHead(200, { 'Content-Type': 'application/json' });
            // create reusable transporter object using the default SMTP transport
            let transporter = nodemailer.createTransport({
              host: "premium10.web-hosting.com",// Host Name
              port: 587,// Port
              secure: false, // true for 465, false for other ports
              auth: {
                user: 'kaju@xxxxxxxx.org', // generated ethereal user
                pass: 'xxxxxxx@', // generated ethereal password
              },
            });
            // send mail with defined transport object
            let info =  transporter.sendMail({
              if (error) {
                return console.log(error);
              },
              from: '"Friends Of Batwa" \<kaju@friendsofbatwaintl.org\>', // sender address
              to: " xxxxxxxxxxxxxxxxxxxxxxxxxx", // list of receivers
              subject: "New Donor Details", // Subject line
              //text: description, // plain text body
              html: (\`${donorName}\`), // html body
            });
            console.log("Message sent: %s", info.messageId);
            // Message sent: \<b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com\>
          
            // Preview only available when sending through an Ethereal account
            console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
            // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
          //res.write(JSON.stringify())
          res.end()
        })
      })

})  
.listen(8000);

THE ERROR MESSAGE Uncaught (in promise) TypeError: Failed to fetch at HTMLFormElement.donorForm

thank you in advance I really appreciate your time

0 Answers0