NodeJs TypeError: First argument must be file descriptor解决方法
2017-03-09 13:52
459 查看
最近在学习NodeJs,在练习一个例子的时候发现老是报:TypeError: First argument must be file descriptor,代码如下:
var fs=require('fs');
var buf=new Buffer('我喜爱编程');
fs.open('./anotherMessage.txt','wx',function(err,fd){
fs.write(fd,buf,0,15,0,function(err,written,buffer){
if(err) console.log('写文件操作失败。');
else console.log("写文件操作成功。");
fs.fsync(fd);
fs.close(fd);
});
});
fs.write方法明明有“fd”这个文件描述,为何偏偏报这个错,原来是因为anotherMessage.txt文件在我本地已经存在了,fs.write这个方法在执行的时候会检测该文件是否已经存在了,如果已经存在便会报这个错误,解决办法也就很简单了,那就是先删掉本地该文件,然后再执行就成功了,如下所示。
TypeError: First argument must be file descriptor
at TypeError (native)
at Object.fs.write (fs.js:760:20)
at E:\ProgramFiles\nodejs\app.js:4:5
at FSReqWrap.oncomplete (fs.js:123:15)
E:\ProgramFiles\nodejs>node app.js
写文件操作成功。
E:\ProgramFiles\nodejs>
var fs=require('fs');
var buf=new Buffer('我喜爱编程');
fs.open('./anotherMessage.txt','wx',function(err,fd){
fs.write(fd,buf,0,15,0,function(err,written,buffer){
if(err) console.log('写文件操作失败。');
else console.log("写文件操作成功。");
fs.fsync(fd);
fs.close(fd);
});
});
fs.write方法明明有“fd”这个文件描述,为何偏偏报这个错,原来是因为anotherMessage.txt文件在我本地已经存在了,fs.write这个方法在执行的时候会检测该文件是否已经存在了,如果已经存在便会报这个错误,解决办法也就很简单了,那就是先删掉本地该文件,然后再执行就成功了,如下所示。
TypeError: First argument must be file descriptor
at TypeError (native)
at Object.fs.write (fs.js:760:20)
at E:\ProgramFiles\nodejs\app.js:4:5
at FSReqWrap.oncomplete (fs.js:123:15)
E:\ProgramFiles\nodejs>node app.js
写文件操作成功。
E:\ProgramFiles\nodejs>
相关文章推荐
- TypeError:First argument must be file descriptor
- Python出现TypeError: file() argument 1 must be encoded string without NULL bytes, not str问题解决
- “The public type XYZ must be defined in its own file”解决方法
- Error parsing XML. org.xml.sax.SAXParseException: Element type "sqlMapConfig" must be declared出错解决方法
- JAVA中 错误代码是 the public type must be defined in its own file 解决方法 android开发 java编程
- [置顶] JAVA中 错误代码是 the public type must be defined in its own file 解决方法 android开发 java编程
- TypeError: unbound method x must be called with x instance as first argument (got nothing instead)
- JAVA中 错误代码是 the public type must be defined in its own file 解决方法 android开发 java编程
- TypeError: unbound method func1() must be called with base1 instance as first argument (got nothing
- TypeError: strptime() argument 0 must be str, not class 'bytes'解决办法
- Error parsing XML. org.xml.sax.SAXParseException: Element type "sqlMapConfig" must be declared出错解决方法
- JMeter学习-027-JMeter参数文件(脚本分发)路径问题:jmeter.threads.JMeterThread: Test failed! java.lang.IllegalArgumentException: File distributed.csv must exist and be readable解决方法
- Python错误解决 TypeError: first arg must be callable
- Error parsing XML. org.xml.sax.SAXParseException: Element type "sqlMapConfig" must be declared出错解决方法
- MPI编译C++程序出现#error "SEEK_SET is #defined but must not be for the C++ binding of MPI" 的解决方法
- python2.7 使用super关键词 报错 TypeError: must be type, not classobj 解决办法
- node-webkit中的requirejs报错问题:path must be a string error in Require.js
- python3-网络编程-TypeError: send() argument 1 must be bytes or buffer, not str异常
- 引入math模块中的log()方法,导致"TypeError: return arrays must be of ArrayType",什么原因?
- BUG!!!======> syncdb 时 “ TypeError: decode() argument 1 must be string, not None ”