我執行shell命令通過編程Node.js的,其結果是true
...字面上<string> true
,但我不能把它比既不boolean
也不string
相媲美。Node.js的標準輸出值不是字符串或布爾
const exec = require('child_process').exec;
exec('docker inspect -f {{.State.Running}} service-redis', (err, stdout, stderr) => {
// here stdout has value "true"
console.log(typeof stdout) // returns "string"
console.log(stdout == 'true') // returns false
console.log(stdout == true) // returns false
});
我試過轉換stdout.toString()
以防萬一,但結果是一樣的。我很困惑。有任何想法嗎?
'stdout'是一個流。 – Scimonster
也許是因爲'\ n'結尾。你嘗試過'stdout.startsWith('true')'? –
@ShreyashSSarnayak,這麼簡單......你是對的! – Milkncookiez