Nodejs:路径必须是字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25561253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:50:03  来源:igfitidea点击:

Nodejs: path must be a string

node.js

提问by Ryan Yiada

I tried to implement some code that uses promise, and I copied some source code from Ghost. But when I ran it, I got an error:

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

The code:

编码:

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)
    });

Error Output

错误输出

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' }

采纳答案by 3y3

Your problem is here:

你的问题在这里:

write = fs.createWriteStream(configFile)

configFile - is uninitialized variable here. You can avoid same problem in future by using some debugger.

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

I recommend you node-inspector

我推荐你节点检查员