0

I have a module.exports with multiple functions inside. What I have understod, it is possible to share a variable to another file. But I cannot seem to get it work.

translator.js

module.exports = {
  translatorfunc: function (message, queryInput, callback) {


    var parameters = {
      text: queryInput
    };

    var parameters = {
      text: queryInput,
      model_id: 'es-en'
    };

    languageTranslator.translate(
      parameters,
      function (error, response) {
        if (error)
          bot.reply(message, 'Cannot find language that should understand this.')//console.log(error)
        else
          var TranslatedOutput = response.translations[0].translation;
        assistant.message({
          input: { 'text': TranslatedOutput }
        }, function (err, response) {
          if (err)
            console.log('error:', err);
          else
            queryOutput = response.output.text[0];
          var parameters = {
            text: queryOutput,
            model_id: 'en-es'
          };
          languageTranslator.translate(
            parameters,
            function (error, response) {
              if (error)
                bot.reply(message, 'Cannot find language that should understand this.')//console.log(error)
              else
                TranslatedOutput = response.translations[0].translation;
            }
          )
        });
      }
    )
  }
}

The variable I'm trying to send to a different file is TranslatedOutput

I also tried to wrap the function languageTranslator.translate as a function, but when I'm calling the variable, it says undefined.

And to get the variable

var translator = require('./tools/translator')
console.log(translator.translatorfunc.TranslatedOutput);
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Dymond
  • 2,158
  • 7
  • 45
  • 80
  • 1
    The translated output doesn't, and can't, exist until you **call** the `translatefunc` function, which calls `languageTranslator.translate`, and its asynchronous callback has been called. So you can't access it immediately after `require`, it doesn't exist yet. Although it would be possible to export a property that got filled in, you wouldn't know *when* you could access it, and multiple calls to `translatefunc` would overwrite each other's results in a chaotic way (since the result is asynchronous). – T.J. Crowder Dec 11 '18 at 14:36
  • I was thinking the same, that maybe the function does not exist yet. So i tried multiple way run it before, but as you say its overwriting thats why i have the problem.. so close. Do you have any suggestion? instead of calling it multiple times? – Dymond Dec 11 '18 at 14:44
  • 1
    Don't call it multiple times. . . It should be possible to arrange your code to wait for asynchronous responses. – Cody G Dec 11 '18 at 14:47

1 Answers1

0

You have to make your variable an attribute of the module.exports object:

module.exports.TranslatedOutput = 'something';

This way you can import it like that:

var TranslatedOutput = require('myModule'). TranslatedOutput;

raphaelSeguin
  • 449
  • 3
  • 12