nodejs child_process spawn не работает, когда я вызываю его из функции - PullRequest
0 голосов
/ 03 сентября 2018

Я пытаюсь поговорить с файлом Python в узле JS. Когда я пишу код ниже, он запускается.

var _data = {
phoneNumber:254727677068,
amount:10
}

var spawn = require('child_process').spawn;
var scriptExecution = spawn("python", ["./nifty.py"]); 
   scriptExecution.stdout.on('data', (data) => {
   console.log("python output",String.fromCharCode.apply(null, data));
});

var data = JSON.stringify([_data.phoneNumber,parseInt(_data.amount),'kbsacco','f861c6cc-4efa-4306-8eee-08f035b03772']);

scriptExecution.stdin.write(data);

scriptExecution.stdin.end();  

но когда я использую его внутри функции и вызываю его, я ничего не получаю ...

makePayment = function(_data){
    // console.log("incoming: ",_data)
    var spawn = require('child_process').spawn;

    var scriptExecution = spawn("python", ["./nifty.py"]); 

   // Handle normal output
   scriptExecution.stdout.on('data', (data) => {
       console.log("python output",String.fromCharCode.apply(null, data));
   });

   var data = JSON.stringify([_data.phoneNumber,parseInt(_data.amount),'kbsacco','f861c6cc-4efa-4306-8eee-08f035b03772']);
   console.log("data: ",data)
   // Write data (remember to send only strings or numbers, otherwhise python wont understand)
   scriptExecution.stdin.write(data);
   // End data write
   scriptExecution.stdin.end();        
}

    socket.on('payLoan',(_data)=>{
        console.log("Here comes loan repayment: ",_data)
        // payments.makePayment(data.phone_number,data.amount) 

        var data = {
            phoneNumber:_data.phoneNumber,
            amount:_data.amount
        }
        makePayment(data)
    })

вызов его внутри socketio ничего не дает

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...