简体   繁体   English

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

[英]Meteor.http method is undefined on server?

So, I'm trying to write a method that makes an http call. 所以,我正在尝试编写一个进行http调用的方法。 When I run the method, I get the following error: 当我运行该方法时,我收到以下错误:

Exception while invoking method 'upload' TypeError: Cannot call method 'call' of undefined 调用方法'upload'时出现异常TypeError:无法调用未定义的方法'call'

Here is what the code looks like: 这是代码的样子:

Client: 客户:

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

Server: 服务器:

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

UPDATE: Problem solved, turns out I had to enable the package: meteor add http 更新:问题解决了,结果我不得不启用包: meteor add http

You simply need to add the HTTP package by running this on command line in your project : 您只需在项目的命令行中运行此命令就可以添加HTTP包:

meteor add http 流星添加http

Also you need a call back using Meteor.call client side. 您还需要使用Meteor.call客户端进行回调。

From the documentation: 从文档:

On the client, if you do not pass a callback and you are not inside a stub, call will return undefined, and you will have no way to get the return value of the method. 在客户端上,如果您没有传递回调并且您不在存根中,则调用将返回undefined,并且您将无法获取该方法的返回值。 That is because the client doesn't have fibers, so there is not actually any way it can block on the remote execution of a method. 这是因为客户端没有光纤,因此实际上没有任何方法可以阻止远程执行方法。

So you should change this 所以你应该改变这个

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

to this 对此

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM