简单的JavaScript代码不适用于nodejs(Simple JavaScript code does not work with nodejs)

系统教程 行业动态 更新时间:2024-06-14 17:03:54
简单的JavaScript代码不适用于nodejs(Simple JavaScript code does not work with nodejs)

我是一名经验丰富的JavaScript程序员,但我刚开始学习node.js。

使用node,我想读取目录的内容,并打印出只有特定扩展名的文件。 目录和文件扩展名都将由命令行参数给出。

但是,我也想在解决这些难题时推动自己并探索JavaScript编程概念,因此我想创建一个File对象来存储有关文件的信息,并使用它来解决问题。

因此,我的方法过于复杂,我知道有更简单的方法可以做到这一点,但我只想找到一个能够解决当前问题的答案:

为什么node.js会抛出以下错误

this.baseName = /.+(?=\.[^.]+)/.exec(file)[0];
                                          ^
TypeError: Cannot read property '0' of null

在这段代码中:

function File(file){
  if (file instanceof File) return file;
  if (!(this instanceof File)) return new File(file);

  if (typeof file !== "string") throw new Error("Useful error." + typeof(file));

  this.fullName = file;

  /*vvvvvvvvvvvv  Important Bit  vvvvvvvvvvvvvv*/
  this.baseName = /.+(?=\.[^.]+)/.exec(file)[0];
  this.extension = /[^.]+(?=$)/.exec(file)[0];
}

File.prototype = {
  isOfType: function(ext){
    return ext === this.extension;
  }
}

var fs = require('fs');
var argv = process.argv;

fs.readdir(argv[2], function(err, list){
  var res = list.filter(function(element, index, array){
    var file = new File(element);
    return file.isOfType(argv[3]);
  });
  console.log(res);
});

Why does node.js throw the following error

this.baseName = /.+(?=\.[^.]+)/.exec(file)[0];
                                          ^
TypeError: Cannot read property '0' of null

in this code:

function File(file){
  if (file instanceof File) return file;
  if (!(this instanceof File)) return new File(file);

  if (typeof file !== "string") throw new Error("Useful error." + typeof(file));

  this.fullName = file;

  /*vvvvvvvvvvvv  Important Bit  vvvvvvvvvvvvvv*/
  this.baseName = /.+(?=\.[^.]+)/.exec(file)[0];
  this.extension = /[^.]+(?=$)/.exec(file)[0];
}

File.prototype = {
  isOfType: function(ext){
    return ext === this.extension;
  }
}

var fs = require('fs');
var argv = process.argv;

fs.readdir(argv[2], function(err, list){
  var res = list.filter(function(element, index, array){
    var file = new File(element);
    return file.isOfType(argv[3]);
  });
  console.log(res);
});
 

but, in a Chrome js console, it runs fine (with simulated process and fs objects of course).

To me (inexperienced me) it looks like node could be making several mistakes:

Not handling the regex properly (I've done tests and this seems likely) Using square brackets to find key '0' within object, instead of index 0 within array.

Or I could be making several mistakes:

Not understanding fs.readdir and its necessary callback. Not understanding possible differences between constructors in JavaScript and Node

Please help, I'd like an answer that solves or explains my current problem, not one that works around it.

Thanks.

最满意答案

Node有一种内置的方法来检查有效文件

function File(file){ if (file instanceof File) return file; if (!(this instanceof File)) return new File(file); if (typeof file !== "string") throw new Error("Useful error." + typeof(file)); var stat = fs.statSync(file); if ( stat && stat.isFile() ) { this.fullName = file; /*vvvvvvvvvvvv Important Bit vvvvvvvvvvvvvv*/ this.baseName = /.+(?=\.[^.]+)/.exec(file)[0]; this.extension = /[^.]+(?=$)/.exec(file)[0]; } }

Node还有一种内置的方式来获取扩展名和路径名

var path = require('path'); var basename = path.basename(file); var extension = path.extname(file);

Node has a built in way to check for valid files

function File(file){ if (file instanceof File) return file; if (!(this instanceof File)) return new File(file); if (typeof file !== "string") throw new Error("Useful error." + typeof(file)); var stat = fs.statSync(file); if ( stat && stat.isFile() ) { this.fullName = file; /*vvvvvvvvvvvv Important Bit vvvvvvvvvvvvvv*/ this.baseName = /.+(?=\.[^.]+)/.exec(file)[0]; this.extension = /[^.]+(?=$)/.exec(file)[0]; } }

Node also has a built in way to get extensions and path names

var path = require('path'); var basename = path.basename(file); var extension = path.extname(file);

更多推荐

本文发布于:2023-04-24 14:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/840fa4e0fc6e186339d365fd3cb739ff.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不适用于   代码   简单   JavaScript   work

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!