Meteor methods, trying to call child_process.spawn, getting TypeError: child_process.spawn is not a function

Meteor methods, trying to call child_process.spawn, getting TypeError: child_process.spawn is not a function

我正在尝试从客户端组件内部调用 Meteor.method:

Meteor.call('execute', this.parameter);

Meteor.methods 有一个生成进程的函数,如下所示:

cp.spawn(pathtoscript, ['-t', parameter.myid], options);

这是一个成功执行的有效进程派生(最多需要 30 秒才能完成),但是浏览器控制台在调用后立即吐出错误:

Exception while simulating the effect of invoking 'execute' TypeError: cp.spawn is not a function(…) TypeError: cp.spawn is not a function

我试过只生成进程并退出函数,我也试过等待 'close' 事件。两次都在后台执行成功,但是浏览器控制台抛出异常。

我也试过异步调用 Meteor.methods

Meteor.call('execute', this.parameter, function(error, result) {
    if (error) {
        alert(error, error.reason);
    }
        console.log(result);
    });*/

同时在 Meteor.method 中添加 return 个值。它总是以同样的方式结束。

能否请您告知在这种情况下产卵过程的正确方法?

这是因为你的方法代码在客户端和服务端都有。它不能在客户端上运行,因为在浏览器中没有spawn

要解决此问题,您只需将您的方法仅移动到服务器代码,或者将其包装在条件为 Meteor.isServer:

if 语句中
if (Meteor.isServer) {
  Meteor.methods({
    execute(params) {
      //...
    }
  });
}