Return database value to e-mail

Asked

Viewed 50 times

2

Hello!

I have a question here, I’m doing a function that takes the data from the Database and sends an email with them, for example: 'Hello {{Name}}'. ({{Name}}) It would be the field that would come from the database.

I managed to get you the value of DB but when I put it in the email, it just looks like Object Promise. I need help with that.

I am using Node.JS and the database is Postgresql

let pool = new Pool (config);

async Function names () { Let destNome = await pool.query('SELECT destinatari_name FROM data') console.log(destNome.Rows[0]. destinatar_name) }

async Function prevEntrega() { Let destPrevEntrega = await pool.query('SELECT previsao_delivery FROM data') console.log(destPrevEntrega.Rows[10]. previsao_delivery) }

names(); prevEntrega();

var mailOptions = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Email teste',
  html: '

Olá, seja bem-vindo' + nomes() + ', a previsão de entrega é' + prevEntrega() + '.

' };

  • You can also enter the code that will use mailOptions?

  • var nodemailer = require('nodemailer'); var Transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: '[email protected]', pass: 'password' } });

1 answer

1


Its function nomes is async, that is, it returns a Promise. So to use it you must use the operator await:

const mailOptions = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Email teste',
  html: '
Olá, seja bem-vindo' + await nomes() + ', a previsão de entrega é' + await prevEntrega() + '.'
};

The return in his duties:

const  pool = new Pool (config);

const nomes = async () => {
  const destNome = await pool.query('SELECT destinatario_nome FROM dados');
  console.log(destNome.rows[0].destinatario_nome);
  return destNome.rows[0].destinatario_nome;
};

const prevEntrega = async () => {
  const destPrevEntrega = await pool.query('SELECT previsao_entrega FROM dados');
  console.log(destPrevEntrega.rows[10].previsao_entrega);
  return destPrevEntrega.rows[10].previsao_entrega;
};

A suggestion to improve data search is to do everything at once:

const pool = new Pool(config);

const buscar = async () => {
  const { rows: { [0]: { destinatario_nome: destinatario, previsao_entrega: previsao } } } = await pool.query('SELECT destinatario_nome, previsao_entrega FROM dados');

  return {
    destinatario,
    previsao,
  };
}

And you will use it as follows:

const { destinatario, previsao } = await buscar();

const mailOptions = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Email teste',
  html: `Olá, seja bem-vindo ${^destinatario}, a previsão de entrega é ${previsao} .`
};

Promise

Promise is an object used for asynchronous processing. An Promise (of "promise") represents a value that may be available now, in the future or never.


await

The operator await is used to wait for a Promise. It can be used only within a function async.

  • I added your code to check if it was correct and based on it, but when I try to send an email, it returns the Unexpected Identifier error. This happened when I put the 'await' before the name().

  • @Eduardonunes the operator await can only be used within a function async. The function you are calling in is async?

  • Oh understood, I had not read the comment above that highlighted it... I was trying to use it in the message that would be sent by email.

  • Now returns the error of 'Await is only Valid in async Function', I believe it is by 'await fetch()'.

  • @Eduardonunes puts the complete file here https://pastebin.com/ and sends me the link for me to look

  • https://pastebin.com/EDyBtELx

  • @Eduardonunes thus attempts https://pastebin.com/16STWkwa

  • It worked out! Thanks brother!

  • @Eduardonunes do not forget to accept the answer so that other people can also benefit from it

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.