Tywin98
Tywin98

Reputation: 35

nodemailer, posting a variable that contains the html body

I'm trying to pass via post a variable that contains some HTML code from a vue component.

I'm using typescript+nuxt.js(+node+vue)

const order_list = document.querySelector('table') as HTMLInputElement | null;
    sendMail(){
      $fetch("/api/prenota/mail",{
        method: "POST",
        body: {
          order_list : this.order_list,
          }
      });
      console.log(this.order_list)
    },
  },

console.log inside the vue component return the exact HTML content that I wanna send to my API.

This is the function inside the API:

export default defineEventHandler(async function(event) {
    const user = decodingUser(event)
    requireLogin(user)
    const { order_list } = await readBody(event)

    let mailOptions = {
    
        to: '[email protected]',
        subject: 'Nice Nodemailer test',
        text: 'AAAAAAAAAAAAAAAA ',
        html: `${{order_list}}`,
    };
    try {
        return await transport.sendMail(mailOptions)
    } catch(err) {
       console.log(err)
       return err
    }
});

The email is being sent, but with this body:

[object Object]

What I'm doing wrong?

Tried to passing the variable forcing it as String, with no results.

Upvotes: 1

Views: 118

Answers (1)

Aleksandr Savkin
Aleksandr Savkin

Reputation: 403

The matter is that you try to pass the HTMLTableElement object through a request.

The console.log tricked you, but then it translated the table to string and you got '[object Object]'. Anyway, handling HTMLTableElement on the backend is a bad approach.

If I understood you properly, you want to pass the HTML code of the table. I think it'll be better to pass the outerHTML property of this object.

const order_list_html = document.querySelector('table').outerHTML;

sendMail(){
  $fetch("/api/prenota/mail",{
    method: "POST",
    body: {
      order_list : this.order_list_html,
    }
  });
  console.log(this.order_list_html)
},

I hope, it was helpful to you.

Upvotes: 1

Related Questions