javascript - 如何在 javascript 中修复 "Error: spawn ENAMETOOLONG"(可能由 jq 引起)?

标签 javascript node.js jq

我在 Windows 10 上,我使用 node.js,我使用 node-jq@1.10.0(目前最新版本)

我想用 jq 从 JSON 中检索一些信息。 所以我使用带有“json”作为选项参数的 jq.run 函数。 但它抛出一个错误,我知道我的查询很好,并且我的所有代码都没有以“let AllKOMoves = ...”开头的行。

我的 JSON 格式正确,我在 JMES 路径中尝试了这个程序,它运行良好,所以 JSON 没有问题。我做了一些研究,有人说我的操作系统不能支持太长的请求。我想准确地说,我的 JSON 重 6Mo。

我在互联网上做了一些研究,其中一些在这个网站上谈论“未指定 .catch(…)”:here这是关于太长查询的话题:here

这是我的代码,我将 const jq = require('node-jq'); 放在了异步函数之外。

(async ()=>{
    let name = "abomasnow"; 
    const fileContent = fs.readFileSync(`./Pokemon with move CSV/Queries/nonStatusMove/${name}nonStatusMove.csv`);
    let pokemonSubject = JSON.parse(await jq.run(`. | map(select(.name == "${name}"))[0]`, './nameBSTypeAbilityWeight.json'));
    var allMoves = moveToJSON(fileContent);
    let AllKOMoves = await jq.run(`. | map(select(.Moves | map(select(.Max == 100)) | length > 1)) | map({Nature: .Nature, EV: .EV, Moves: [.Moves | map(select(.Max == 100))[] | .Move], MovesCount: .Moves | length})`,getAllInflictedDamage(pokemonSubject, allMoves, pokemonJSON[0]),{ input: 'json' })
    console.log(AllKOMoves);
    })();

下面是这部分代码 getAllInflictedDamage(pokemonSubject, allMoves, pokemonJSON[0]) 生成的 JSON 示例:

[  
   {  
      "Opponent":"abomasnow",
      "Move":"wood-hammer",
      "Min":16.15,
      "Max":19.25,
      "Crit":28.88
   },
   {  
      "Opponent":"abomasnow",
      "Move":"round",
      "Min":15.84,
      "Max":18.94,
      "Crit":28.26
   }
]

这是变量 allMoves 中包含的 JSON 示例:

[  
   {  
      "name":"bulldoze",
      "accuracy":"100",
      "category":"physical",
      "power":"60",
      "priority":"0",
      "target":"all-other-pokemon",
      "type":"ground"
   },
   {  
      "name":"frost-breath",
      "accuracy":"90",
      "category":"special",
      "power":"60",
      "priority":"0",
      "target":"selected-pokemon",
      "type":"ice"
   }
]

我期待我的 JSON 的内容,但它抛出了这个错误消息:

(node:21792) UnhandledPromiseRejectionWarning: Error: spawn ENAMETOOLONG
    at ChildProcess.spawn (internal/child_process.js:366:11)
    at Object.spawn (child_process.js:538:9)
    at C:\Users\adela\node_modules\node-jq\lib\exec.js:24:43
    at new Promise (<anonymous>)
    at exec (C:\Users\adela\node_modules\node-jq\lib\exec.js:20:10)
    at C:\Users\adela\node_modules\node-jq\lib\jq.js:24:24
    at new Promise (<anonymous>)
    at Object.run (C:\Users\adela\node_modules\node-jq\lib\jq.js:19:10)
    at C:\Users\adela\node_code\calcDamage1forAll.js:757:28
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:21792) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:21792) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

通常我的代码应该像这样检索信息:

[  
   {  
      "Nature":"brave",
      "EV":[  
         4,
         0,
         0,
         0,
         252,
         252
      ],
      "Moves":[  
         "focus-punch",
         "natural-gift-fire90",
         "natural-gift-fire100",
         "natural-gift-fire80"
      ],
      "MovesCount":4
   },
   {  
      "Nature":"brave",
      "EV":[  
         0,
         252,
         252,
         4,
         0,
         0
      ],
      "Moves":[  
         "natural-gift-fire90",
         "natural-gift-fire100",
         "natural-gift-fire80"
      ],
      "MovesCount":3
   }
]

编辑:如果我只这样做:

let AllKOMoves = await jq.run(`.`,getAllInflictedDamage(pokemonSubject, allMoves, pokemonJSON[0]),{ input: 'json' })

它也给我同样的错误!

EDIT2:如果我这样做:

let AllKOMoves = await jq.run(`.`,{"foo":"bar"},{ input: 'json' })

它工作得很好!通过一些研究,我认为它只是关于 json 的大小,因为我的 json 大小 ~12Mo,我尝试使用 300 Ko json 它也不起作用所以我认为我的 json 太大了。

我现在的问题是,是否存在其他解决方案可以像第一个答案中提出的那样创建临时文件...

最佳答案

编辑:

由于这个问题,现在已经在 node-jq 1.10.0 ( pull #191 ) 中重新检查并解决了这个问题,因此请确保 node-jq 的版本是最新的您的项目的依赖项应该可以防止出现此错误。

node-jq 1.10.0 之前的背景(或正在研究 ENAMETOOLARGE 问题的人员)

input: 除了'file' 的问题在于它 gets translated by node-jq versions before 1.10.0进入命令行,如 jq --null-input '[json] | [filter]' 因此您的 JSON 和过滤器组合必须小于您的操作系统可接受的最大单个参数和/或总参数限制,在 Windows 上这可能小至 8192 字节。 (对于 128-256k 左右的参数,大多数 UNIX 克隆应该失败并返回 an E2BIG error instead。)

要通过早期版本的 node-jq 将大量数据传递给 jq 而不是直接通过命令行参数,您需要写入一个名称以 .json 结尾的临时文件以使用 {input:file} 如果它很大。

虽然理论上,您可以为文件提供一个 [] 数组,让 jq 通过 stdin 等待数据,node-jq 包装子进程,所以它会丑陋地处理它的标准输入。因此,它需要修复,或者需要编写一个备用包装器以通过标准输入将数据传递给 jq,例如:

child_process.spawn... 
process.stdin.write('myJsonAsAstring')
process.stdin.end()

更多讨论可见原文node-jq issue .

关于javascript - 如何在 javascript 中修复 "Error: spawn ENAMETOOLONG"(可能由 jq 引起)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57166113/

相关文章:

javascript - Lambda 未将 http 响应正文返回到 API 网关

MVC 场景中的 Javascript 事件与回调

javascript - 扩展 MooTools

node.js - 如何让 i18next-node 以正确的方式显示变音符号?

json - 打印缺少键的空字符串并将结果转换为 CSV

json - 使用 jq 处理巨大的 json 数组文件

javascript - 使用 javascript 和 PhoneGap 的 HTML5 移动应用程序本地化

node.js - MongoDB 如何捕获deleteMany中的错误

javascript - 当并发请求在网络服务器中命中时,如何编写基于计数的逻辑(NodeJS)

json - 使用 jq 有条件地将元素添加到 json 数组