2016-09-17 54 views
-1

我怎樣才能從內容:的NodeJS READFILE獲取內容

fs.readFile('./access.log', function read(err, data) { 
var content = data.toString(); 
}); 

我需要在其他功能使用它。

當我tryied這樣的:

var content; 
fs.readFile('./access.log', function read(err, data) { 
content = data.toString(); 
}); 

console.log("Content: " + content); 

這表明內容是不確定的。

什麼是最佳解決方案?

+1

你不能把異步函數,就好像它們是同步的。關於節點已經討論過這一點。嘗試搜索這裏和谷歌,你會發現很多,*很多*的結果。 – mscdex

+0

./access.log文件位置不可訪問 –

+0

我檢查,當我把console.log(內容)功能,它顯示此文件的內容 – divine

回答

0

的soluton是

var text = fs.readFileSync('./access.log', 'utf8');