带有承诺放大缓冲区的节点js getasync

node js getasync with promise enlarge buffer

我正在使用 getasync 执行一些 cmd 命令,它给我这个错误:

Unhandled rejection RangeError [ERR_CHILD_PROCESS_STDIO_MAXBUFFER]: stderr maxBuffer length exceeded

输出相当大,但我需要它来调试底层python脚本。有没有办法扩大maxBuffer?看我的代码:

thecaller: function(req, callback) {

const geoJSON = req.geoJSON;
const nameofinstance = req.name;
const Promise = require("bluebird");
const cmd = require('node-cmd');
const getAsync = Promise.promisify(cmd.get, { multiArgs: true, context: cmd });
getAsync(`python3 firstscript.py`)
  .then(() => getAsync(`python3 secondscript.py`))
  .then(callback(nameofplanung));

问题出现在第一个“.then”

除了使用 node-cmd,您还可以执行以下操作

https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

你要设置的是maxBuffer选项。

Example slightly modified:

const { exec } = require('child_process');
const execAsync = function(cmd){ return new Promise((res,rej)=>{
  exec(cmd,{maxBuffer:4*1024*1024}, (error, stdout, stderr) => {
    if (error) {
      console.error(`exec error: ${error}`);
      rej(err)
    }
    console.log(`stdout: ${stdout}`);
    console.error(`stderr: ${stderr}`);
    res(stdout);
  });
})}
execAsync('ls').then(r=>...).catch(e=>...)