javascript - NodeJS - Storing command line output as a var and returning it to client via res.send() -


i working on end of application, using nodejs. part of code, read first line of file , send client using res.json. code looks this

var hr = 'head -n 1 ../' + req.file.path + '_hr.txt';  exec(hr, function (error, stdout, stderr) {     console.log(hr);     console.log(stdout);     console.log(stderr);     res.json({       "heartrate": stdout     });   }) 

when execute however,

{"heartrate" : ""} 

even though on console of end see value stdout.

i have looked @ other related questions information i've got has been in bits , pieces. realise object produced stdout not string. tried tostring() method on didn't work.

i put if(stdout) around res.json since exec ansynchronous , may stdout may not have been written @ point when call console.log(stdout), did not work.

i tried using spawn instead of exec no avail, although may have used wrongly. i'm sure solution problem simple, have not been able find it. appreciated!

so turns out logging wrong stdout, logged right 1 , got answer wanted


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -