Meteor.http 方法在服务器上未定义

Meteor.http method is undefined on server?

本文关键字:未定义 服务器 http 方法 Meteor      更新时间:2023-09-26

所以,我正在尝试编写一个进行http调用的方法。当我运行该方法时,出现以下错误:

调用方法"上传"时出现异常 类型错误:无法调用未定义的方法"调用"

代码如下所示:

客户:

console.log(Meteor.call('upload', f, content));

服务器:

Meteor.methods({
  upload: function(file, content) {
    this.unblock();
    Meteor.http.call("PUT", "http://blah");
  }
});

更新:问题解决了,原来我必须启用该软件包:meteor add http

您只需要在项目中的命令行上运行以下命令来添加 HTTP 包:

流星添加 http

此外,

您需要使用 Meteor.call 客户端进行回调。

从文档中:

在客户端上,如果不传递回调并且不在存根内,则调用将返回 undefined,并且您将无法获取方法的返回值。这是因为客户端没有纤程,因此实际上没有任何方法可以阻止方法的远程执行。

所以你应该改变这个

console.log(Meteor.call('upload', f, content));

对此

Meteor.call('upload', f, content, function(error, result){console.log(result);});