Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
Using child process output globally

For a project I need to incorporate a backend Python function with Javascript (main code for a chatbot). Using Child processes, it seems to work (when using 'node script.js'). However, I need to access the data from the called python function. Right now, all I am getting is the the output.I tried to store it in the global variable, but it's showing as 'undefined'. Is there a way to actually access the data so I can use it outside the stdout.on?

This is the Javascript code for running the pythonscript:

// Give a path to the QR scanner Python file
const qrScannerPath = "python/qrCodeScanner.py"
const base64Arg = "base64_2.txt"

// Provide the '.exe' python file. If python is available as an 'environment varaible', then simply refer to it as 'python'"
const pythonExe = "python"

// Function to convert a utf-8 array to a string
const utfConverter = function (data) {
  return  String.fromCharCode.apply(String,(data))
}

// let's us handle python scripts
const spawn = require("child_process").spawn
const scriptExe = spawn(pythonExe, [qrScannerPath, base64Arg])
// If all goes well, the program should execute the Python code
let counterpartyData = {}
scriptExe.stdout.on("data", function (data) {

    console.log("getting the Python script data...")
    let cp = JSON.parse(utfConverter(data))
    counterpartyData = {... cp} //should store the data to the variable

});
console.log(counterpartyData) // shows 'undefinied"

// In case our python script cannot be run, we'll get an error
scriptExe.stderr.on("data", (data) => {
    console.error("error : " + data.toString())
    
});


//logs error message
scriptExe.on('error', (error) => {
    console.error('error: ', error.message);
});
    
// Logs a message saying that our code worked perfectly fine
scriptExe.on("exit", (code) => {
  console.log("Process quit with code : " + code)
})

If I run this code with node, the output of 'counterpartyData' is undefined. However, inside the stdout, it actually prints out the data I want.

Furthermore, I get python import errors when running the app on Heroku :(.

Thank you in advance!!

Happy New Year and joyful greetings <3

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda