node.js - 无法读取 NodeJS 中未定义的属性“F_OK”
问题描述
我正在尝试在 NestJS 中使用 ExcelJS。由于某种原因,我无法打开 xlsx 文件。
exceljs 使用此代码检查文件是否存在
fs: {
exists: function exists(path) {
return new Promise(function (resolve) {
console.log(path);
fs.access(path, fs.constants.F_OK, function (err) {
resolve(!err);
});
});
}
},
该代码返回无法读取未定义的属性“F_OK”。第 4 行的 Console.log 返回我正确发送的路径
但是,如果我尝试使用此代码从我的服务访问相同的文件
fs.access(this.file, fs.constants.F_OK | fs.constants.W_OK, (err) => {
if (err) {
console.error(
`${this.file} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`);
} else {
console.log(`${this.file} exists, and it is writable`);
}
});
该代码返回“file.xlsx 存在,并且它是可写的”。有什么区别,为什么我不能用 exceljs 读取同一个文件?
解决方案
我有同样的问题。
原因是因为我正在导入fs/promises
并且constants
存储在fs
基本上,您正在尝试constants
从未声明它们的地方获取。
我就是这样解决的:
import fsp from 'fs/promises'
import fs from 'fs'
await fsp.access(testDir, fs.constants.F_OK);
你可以看到 for access
I usefsp.access
和 for theconstants
fs.constants.F_OK
推荐阅读
- javascript - 输出奇怪的主任务和微任务的执行顺序
- azure-web-app-service - 在一个应用服务计划下托管多个网站的 Azure 部署槽
- javascript - 为什么我的图像滑块上的 background-size 或 background-repeat 属性不起作用?
- javascript - 我有一个 JS 函数,它应该根据 DropDownListFor 中的事件更改来更改 td 的 innerHTML,但仅适用于初始加载
- excel - 汇总时间记录以准确反映小时:分钟:秒
- r - r rvest 错误:“doc_namespaces(doc) 中的错误:外部指针无效”
- sql - 如何在 SQL Server 中的两个日期之间进行选择
- android - 使用 diff utils 更新回收器视图适配器,添加项目时无法正常工作
- node.js - docker-compose rabbitmq 服务端口覆盖我的节点快速端口
- r - 如何查找包是如何在 R 中加载的?