Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我总是在node.js中得到ReferenceError_Node.js - Fatal编程技术网

为什么我总是在node.js中得到ReferenceError

为什么我总是在node.js中得到ReferenceError,node.js,Node.js,我正在阅读Smashing Node.JS这本书,在执行文件资源管理器示例时,我不断遇到一个引用错误。发生这种情况的原因以及我如何纠正该问题。我遵循了书中的例子,所以我对发生的事情有点不知所措 /** * Module dependencies. */ var fs = require('fs') , stdin = process.stdin , stdout = process.stdout; fs.readdir(proces

我正在阅读Smashing Node.JS这本书,在执行文件资源管理器示例时,我不断遇到一个引用错误。发生这种情况的原因以及我如何纠正该问题。我遵循了书中的例子,所以我对发生的事情有点不知所措

    /**
* Module dependencies. 
*/ 

    var fs = require('fs')
        , stdin = process.stdin
        , stdout = process.stdout;

    fs.readdir(process.cwd(), function (err, files) {
      console.log('');

     if (!files.length) {
      return console.log('  \033[31m No files to show!\033[39m\n'); 
     } 

      console.log(' Select which file or directory you want to see\n');

      function file(i) {
    var filename = files[i]; 

    fs.stat(__dirname + '/' + filename, function (err, stat) {
      if (stat.isDirectory()) {
        console.log('   '+i+'   \033[36m' + filename + '/\033[39m'); 
      } else {
        console.log('       '+i+'   \033[90m' + filename + '\033[39m')
      }

      if (++i == files.length) {
        read();
      } else{
        file(i); 
      }
    });
  }

  file(0); 
});

function read() {
    console.log(''); 
    stdout.write('  \033[33mEnter your choice: \033[39m');

    stdin.resume(); 
    stdout.setEncoding('utf8'); 

    stdin.on('data', option); 

    function option( data ) {
        if (typeof files[Number(data)] !== "undefined" ) {
            stdout.write('  \033[31mEnter your choice: \033[39m');
        } else {
            stdin.pause(); 
        }
    }

}
调用
read()
时,它无权访问
文件
,因此在使用
文件类型[…]时出现引用错误

一个想法可能是移动
})
文件(0)
之后到文件的底部,因此已读入
fs.readdir(process.cwd(),函数(err,files){
块,该块定义
文件


然而,我真的希望这个例子能在你的书中展开:现在,它不会输出你选择的目录的目录内容,而是会提示你反复输入一个数字。

交替地将文件传递给read函数

请注意,示例代码真的非常糟糕,我希望作者试图对此加以说明,而不是将其作为好代码

因此,继续使用坏代码,这里是一个完整的工作示例:

var fs = require('fs')
    , stdin = process.stdin
    , stdout = process.stdout;

fs.readdir(process.cwd(), function (err, files) {
    console.log('');

    if (!files.length) {
        return console.log('  \033[31m No files to show!\033[39m\n');
    }

    console.log(' Select which file or directory you want to see\n');

    function file(i) {
        var filename = files[i];

        fs.stat(__dirname + '/' + filename, function (err, stat) {
            if (stat.isDirectory()) {
                console.log('   '+i+'   \033[36m' + filename + '\033[39m');
            } else {
                console.log('       '+i+'   \033[90m' + filename + '\033[39m')
            }

            if (++i == files.length) {
                read(files);
            } else{
                file(i);
            }
        });
    }

    file(0);
});

function read(files) {
    console.log('');
    stdout.write('  \033[33mEnter your choice: \033[39m');

    stdin.resume();
    stdout.setEncoding('utf8');

    stdin.on('data', option);

    function option( data ) {
        var filename = files[Number(data)];
        if (typeof filename !== "undefined" ) {
            stdout.write('\n\033[90m' + filename + ':\n\n');
            var fileContents = fs.readFileSync(filename)
            stdout.write(fileContents);
            stdout.write('\033[39m\n\n');

            stdout.write('  \033[31mEnter your choice: \033[39m');
        } else {
            stdin.pause();
        }
    }
}