2014-08-29 123 views
6

我試圖實現一些使用promise的代碼,並且我從Ghost複製了一些源代碼。但是,當我跑了,我得到了一個錯誤:Nodejs:路徑必須是字符串

代碼:

var Promise = require('bluebird') 
var fs = require('fs') 
var path = require('path') 
var configPath = path.join(__dirname, '/config-example.js') 
var configFile 

function writeConfigFile(){ 
    return new Promise(function(resolve,reject){ 
     var read, 
      write, 
      error; 
     console.log('path->', configPath) 
     read = fs.createReadStream(configPath); 
     read.on('error', function(err){ 
      console.log('Error->', err); 
      reject(err) 
     }) 

     write = fs.createWriteStream(configFile) 
     write.on('error', function(err){ 
      console.log('Error->',err) 
      reject(err) 
     }) 
     write.on('finish', resolve) 
     read.pipe(write) 
    }); 
} 

var p = writeConfigFile(); 
    p.then(function(data){ 
     console.log(data) 
    },function(data){ 
     console.log('data->',data) 
    }); 

錯誤輸出

path-> /mnt/share/Learn/config-example.js 
data-> [TypeError: path must be a string] 
Error-> { [Error: ENOENT, open '/mnt/share/Learn/config-example.js'] 
errno: 34, code: 'ENOENT', 
path: '/mnt/share/Learn/config-example.js' } 

回答

3

你的問題在這裏:

write = fs.createWriteStream(configFile) 

configFile - 在這裏是未初始化的變量。 您可以通過使用某些調試器來避免將來出現同樣的問題。

我推薦你node-inspector

相關問題