Nodejs:path必须是一个字符串

Rya*_*ada 7 node.js

我尝试实现一些使用的代码promise,并从Ghost复制了一些源代码.但当我运行它时,我收到一个错误:

代码:

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)
    });
Run Code Online (Sandbox Code Playgroud)

错误输出

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' }
Run Code Online (Sandbox Code Playgroud)

3y3*_*3y3 3

你的问题在这里:

write = fs.createWriteStream(configFile)
Run Code Online (Sandbox Code Playgroud)

configFile - 这里是未初始化的变量。您可以通过使用一些调试器来避免将来出现同样的问题。

我推荐你节点检查器