I was using spawn to run a python script from a javascript function but it is not working.
const getUserById = async (req, res) => {
const user = await Users.findOne({chatId: req.params.chatId});
const result = await JSON.parse(JSON.stringify(user));
console.log(result.chatName, result.chatId);
/* result.temp = `temp_${result.chatId}`; */
const { spawn } = require('child_process');
const python = spawn('python', ['temp.py', user.chatName, user.chatId]);
python.stdout.on('data', function (data) {
console.log(data.toString());
result.message = data.toString();
res.json(result);
});
}
temp.py file:
import sys
name = sys.argv[1]
chatID = sys.argv[2]
sys.stdout.write('{a} {b}'.format(a=name, b=chatID))
But independently they are working perfectly. What is the solution?
Note: They are present in the same directory. result.chatName and result.chatId are also getting logged to the console, and the main issue is occurring after that.