Return value of a NODE function

Asked

Viewed 52 times

-2

Hello

I’m starting with NODE. I have the function below and would like it to return the translated text. Today she plays the text to the console. How do I get her to return the text? How would it look? Thanks.

function traduzirTexto() {
  // ==> Aqui vamos configurar os requests
  let options = {
    method: 'POST',
    baseUrl: endpoint,
    url: 'translate',
    qs: {
      'api-version': '3.0',
      'to': ['en', 'es']
    },
    headers: {
      'Ocp-Apim-Subscription-Key': subscriptionKey,
      'Content-type': 'application/json',
      'X-ClientTraceId': uuidv4().toString()
    },
    body: [{
      'text': 'Olá Desenvolvedor!'
    }],
    json: true,
  }

  // ==> Aqui vamos imprimir a nossa requisição
  request(options, (err, res, body) => {
    console.log(JSON.stringify(body, null, 4));
  })
};

Guys I asked what would change the code to work, that’s all I wanted to know if anyone knew, to expedite a project here. I didn’t ask about where to get callback tutorials.

1 answer

-2

Leave the function I needed here in case anyone needs it:

async t(texto) {

    if (this.idioma !== 'pt') {

        const res = await axios({
            baseURL: process.env.TRANSLATOR_TEXT_ENDPOINT,
            url: '/translate',
            method: 'post',
            responseType: 'json',
            headers: {
                'Ocp-Apim-Subscription-Key': process.env.TRANSLATOR_TEXT_SUBSCRIPTION_KEY,
                'Ocp-Apim-Subscription-Region': process.env.TRANSLATOR_TEXT_REGION,
                'Content-type': 'application/json',
                'X-ClientTraceId': process.env.TRANSLATOR_TEXT_ID
            },
            params: {
                'api-version': '3.0',
                'from': 'pt',
                'to': [this.idioma]
            },
            data: [{
                'text': texto
            }]
        });
        texto = res.data[0]['translations'][0].text;            
    }

    return texto;
}

Browser other questions tagged

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