node.js - NodeJs fs.close() TypeError: fd must be a file descriptor
问题描述
我使用 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)
解决方案
我忘记在 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')
})
})
})
推荐阅读
- html - 在 React (NextJS) 和 Tailwind CSS 中切换底层表格行的可见性
- google-apps-script - 如何更改 google-app-scripts (GMAIL) 中文本的颜色、大小和对齐方式等字体样式?
- blender - 自动减小 obj 文件的大小
- python - 多边形类:查找矩形和三角形的面积和长度
- boost-spirit - 使用 boost::spirit 解析器解析单个空格
- php - 子数组集合未与 foreach 一起显示
- azure - Azure KeyVault 机密分组
- javascript - tvOS TVML 应用程序 - 外部 WebVTT 字幕轨道
- typescript - 具有未知参数的通用函数参数
- reactjs - ts(2339) 通用 useFetchData 挂钩出错