1

I'm trying to access a local variable outside the function since a few hours. I don't know where my mistakes is. The code looks like:

Edited code:

  if (lastMsg.toUpperCase().indexOf("@TEST") > -1) { 
     var myPythonScriptPath = 'my_script.py';
     var myMessage = ''; 

      // Use python shell
      const {PythonShell} = require("python-shell");
      var pyshell = new PythonShell(myPythonScriptPath);

      pyshell.on('message', function (message) {
          // received a message sent from the Python script (a simple "print" statement)
          console.log(message);
          myMessage = message;
      });

      // end the input stream and allow the process to exit
      pyshell.end(function (err) {
          if (err){
          throw err;
          };

      });
          sendText = `${myMessage};`

As a result, the variable ${message} is "undefined". The code works by itself, but inside the if statement I can't see the output of the message. How to fix that?

  • 1
    `message` variable is defined inside the callback of `pyshell.on()` function but you're trying to access it outside its scope and that's why `sendText` takes the value `undefined`. – Ajay Dabas Jan 01 '20 at 04:55
  • Thanks, how can I solve this? –  Jan 01 '20 at 04:57
  • 1
    That depends on what you want to do exactly. If you just want the value of `message` available outside `pyshell.on()`, you can defined a variable `var myMessage = ''` and then inside the callback of `pyshell.on()`, assign it the value you get in callback `function (message){ myMessage = message; }` – Ajay Dabas Jan 01 '20 at 05:00
  • That's exactly what I need! I just want the value of message. But I got ";" back after using it. Did I do something wrong? Edited the code –  Jan 01 '20 at 05:09
  • Why is this tagged both java and javascript? – FailingCoder Jan 01 '20 at 05:19
  • removed java as tag. my bad –  Jan 01 '20 at 05:22

2 Answers2

0

message is a variable inside of pyshell.on() and it's obvious that message is undefined outside of this block. if you want to use the value of message, you have to declare a variable outside pyshell.on() and in pyshell.on() fill that with message value.

var message="";
pyshell.on('message', function (message) {
      // received a message sent from the Python script (a simple "print" statement)
      console.log(message);
  });
  • even you can declare a variable with different name and fill it with message value –  Jan 01 '20 at 05:10
  • Thanks for your time, but I don't get any output with this method. Tried that already. Inside that `if` statement I can't access the variable... –  Jan 01 '20 at 05:17
  • 1
    did your local variable get value inside pyshell.on()? –  Jan 01 '20 at 05:27
  • Good question. I'm simply using a print function as python script. Without that `if` statement, I could see the printed output (I think it's stored in console.log?). Or do I need to do something else? –  Jan 01 '20 at 05:31
  • That's the whole part of this variable, there's nothing else related to the `message` function –  Jan 01 '20 at 05:34
0

Apparently, sendText = `${myMessage}`; is being executed before the message is received from the listener pyshell.

pyshell.on('message', function (message) {
      console.log(message);
      //decide here what you want to do with the message
});

Have a look at MDN docs to understand how the asynchronous code behaves.

Update:

The trick lies in waiting until the message is received then return it, a simple solution would be in wrapping the logic in asynchronouse function and trigger a callback once the message is received.

const { PythonShell } = require("python-shell");
var pyshell = new PythonShell(myPythonScriptPath);

function getShellMessage(callback) {
  pyshell.on('message', function (message) {
    console.log(message);
    // end listener here pyshell.end() ?
    callback(null, message);
  });

  // end the input stream and allow the process to exit
  pyshell.end(function (err) {
    if (err){
      callback(err);
    };

  });
}


// the callback function will only get triggered upon receiving the message
getShellMessage(function(err, message) {
  //message is ready
  console.log(message)
});
MAS
  • 706
  • 1
  • 5
  • 14
  • That makes sense. I read the docs, but can't find a solution for this. Is there a way to fix it? –  Jan 01 '20 at 05:40
  • I'll not waste my time with the variables and try to get another way. Thank you very much! –  Jan 01 '20 at 05:47
  • depends on the use case, are you expecting a single message? or a stream of messages? – MAS Jan 01 '20 at 05:48
  • I'm expecting 5 lines of text output / strings. It's just one message. –  Jan 01 '20 at 05:49
  • @robscure answer updated – MAS Jan 01 '20 at 06:04