【问题标题】:Cloud Functions for Firebase: spawn ImageMagick syntaxFirebase 的云函数:生成 ImageMagick 语法
【发布时间】:2017-12-08 11:33:20
【问题描述】:

我想通过云功能使用 imagemagick 获取上传到 Firebase 存储的图像的高度和宽度。

我写了以下代码:

var child = spawn('identify', ["-ping","-format","`%w %h`",tempLocalFile]);
child.stdout.on('data', function(data) {
    console.log('stdout: ' + data);
    //Here is where the output goes
});
child.stderr.on('data', function(data) {
    console.log('stderr: ' + data);
    //Here is where the error output goes
});

但 firebase 日志显示错误:

Cannot read property 'on' of undefined
at mkdirp.then.then (/user_code/index.js

请建议如何编写 Imagemagick 并获取执行的输出。

【问题讨论】:

  • 您的错误显示“mkdirp”有问题,但我在您显示的代码中的任何地方都看不到。您确定问题不在未显示的代码中的其他地方吗?

标签: node.js firebase firebase-storage google-cloud-functions


【解决方案1】:

如果您使用('child-proces-promise').spawn 作为firebase thumbnail sample,您必须注意到它返回一个promise,并且可以使用childProcess 属性访问子进程。

var promise = spawn('identify', ["-ping","-format","`%w %h`",tempLocalFile]);
var child = promise.childProcess;

child.stdout.on('data', function(data) { // do something });
child.stderr.on('data', function(data) { // do something });

【讨论】:

    【解决方案2】:

    您的问题Cannot read property 'on' of undefined 源于您添加stdoutstderr 侦听器的方式。您必须获取进程并将侦听器添加到 that,而不是直接添加到 spawn 返回的承诺中。代码sn-p:

    var spawnPromise = spawn('identify', ["-ping","-format","`%w %h`",tempLocalFile]);
    var childProcess = spawnPromise.childProcess;
    childProcess.stdout.on('data', function(data) {
        console.log('stdout: ' + data);
        //Here is where the output goes
    });
    childProcess.stderr.on('data', function(data) {
        console.log('stderr: ' + data);
        //Here is where the error output goes
    });
    

    【讨论】:

      猜你喜欢
      • 2018-11-27
      • 2021-05-20
      • 1970-01-01
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-21
      • 1970-01-01
      相关资源
      最近更新 更多