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

165
Vistas
Cannot return json from function in a nodejs app that does with a python?

I think that some asynchronous stuff is happening that I cannot figure out Please tell how can I solve this with async-await if possible.

my code:

const {spawn} = require('child_process')


const getWeatherReport = (location) => {
    let dataToSend, result
    const python = spawn('python', [__dirname+'\\weathergetter.py', location.toString()])

    python.stdout.on('data', (data) => {
        console.log('Pipe data from python script ...');
        dataToSend = JSON.parse(data.toString().replace(/\'/g, '\"'));
        // console.log(dataToSend)
    })
    
    python.stderr.on('data', data => {
        console.error(`stderr: ${data}`)
    })
  
    python.on('close', (code) => {
        console.log(`child process close all stdio with code ${code}`);
        // console.log(dataToSend)
    })
    return dataToSend
    
}

var wea = getWeatherReport("kolkata")
console.log(wea)

Output:
undefined
Pipe data from python script ...
child process close all stdio with code 0

I want the function to return a json object which is assigned to dataToSend. Here I am actually working with a python script.

the python script has no problem. When I console.log(dataToSend) inside python.stdout.on() and python.on() as commented out in the code, the json objects gets printed to the console. But I cannot make the function return the json object.

Please refer to these for what I was following:

  • mainly this one -- https://medium.com/swlh/run-python-script-from-node-js-and-send-data-to-browser-15677fcf199f
  • https://www.sohamkamani.com/blog/2015/08/21/python-nodejs-comm/
  • https://towardsdatascience.com/python-pandas-in-node-js-application-47a76ae42a2a
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