简体   繁体   English

TypeError:使用Node.js fs.read()的错误参数

[英]TypeError: Bad argument using Node.js fs.read()

I'm trying to get fs.read() to work but running into some issue(s). 我正在尝试使fs.read()工作,但遇到一些问题。 Here is my fread.js script: 这是我的fread.js脚本:

var fs = require('fs');

fs.open('test.txt', 'r', function (err, handle) {

    var f = handle;
    var b = new Buffer(100000);

    fs.read(f, b, 0, 100000, null, function (err, bytesRead) {
        console.log(b.toSting("utf8", 0, bytesRead));
        fs.close(f);
    });

});

Why do I get the following TypeError: Bad Argument error upon running it? 为什么在运行它时出现以下TypeError: Bad Argument错误?

$ node fread.js 

fs.js:457
  binding.read(fd, buffer, offset, length, position, wrapper);
          ^
TypeError: Bad argument
    at Object.fs.read (fs.js:457:11)
    at /home/max/dev/livelessons/fread.js:8:5
    at Object.oncomplete (fs.js:107:15)

The problem was that I gave it the wrong file name. 问题是我给了它错误的文件名。 test.txt should have been text.txt . test.txt应该是text.txt Doh! h!

You should check that fs.open() was successful first. 您应该先检查fs.open()是否成功。 Most likely err is set and handle is set to undefined, causing the "Bad Argument" error. 很有可能设置了err并且将handle设置为undefined,从而导致“ Bad Argument”错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM