NodeJ fs.close()TypeError:fd必须是文件描述符

时间:2018-12-10 05:02:47

标签: node.js

我正在使用nodejs写入文件。

fs.open(path.join(reportPath), 'a', 666, ( e, fd ) => {
   fs.write( fd, `There is a company named ${companyName}` + os.EOL, null, 'utf8', () => {
      fs.close( (err) => {
        if(err) throw err;
        console.log('write successfull')
     })
   })
 })

它返回一个错误,实际上是文件被写入,但是错误使我的服务器停止。由于throw err

此处是错误消息:

TypeError: fd must be a file descriptor
    at Object.fs.close (fs.js:608:11)
    at C:\DATA\source\code\build\modules\desc\controller.js:199:42
    at FSReqWrap.wrapper [as oncomplete] (fs.js:685:5)

1 个答案:

答案 0 :(得分:2)

我忘了在fs.close()上传递文件描述符

fs.open(path.join(reportPath), 'a', 666, ( e, fd ) => {
   fs.write( fd, `There is a company named ${companyName}` + os.EOL, null, 'utf8', () => {
      fs.close(fd, (err) => {
        if(err) throw err;
        console.log('write successfull')
     })
   })
 })