admin管理员组

文章数量:1178529

So, I'm trying to write a method that makes an http call. When I run the method, I get the following error:

Exception while invoking method 'upload' TypeError: Cannot call method 'call' of undefined

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

So, I'm trying to write a method that makes an http call. When I run the method, I get the following error:

Exception while invoking method 'upload' TypeError: Cannot call method 'call' of undefined

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

Share Improve this question edited Oct 3, 2016 at 23:36 Giacomo1968 26.1k11 gold badges76 silver badges105 bronze badges asked Apr 21, 2012 at 23:25 MarkMark 41k11 gold badges45 silver badges49 bronze badges 6
  • The expression Meteor.http evaluates to undefined ... where's the issue/question? A question would be: "Why is Meteor.http not a function?" or some such. This is just debugging.) – user166390 Commented Apr 21, 2012 at 23:52
  • docs.meteor.com/#meteor_http_call – Mark Commented Apr 22, 2012 at 0:03
  • Are there any errors in your browser's console? – Pavel Strakhov Commented Apr 22, 2012 at 0:06
  • No, there are no errors except for the one i included above, which is outputted by the server – Mark Commented Apr 22, 2012 at 0:07
  • @MarkF Either 1) The API is a liar 2) Meteor (or the correct version) is not loaded correctly 3) Something set Meteor.http to undefined later. Try it in a web/JS-console: Meteor.http <-- what does that result in? It'll be undefined so ... why? Where was it defined? – user166390 Commented Apr 22, 2012 at 0:07
 |  Show 1 more comment

2 Answers 2

Reset to default 41

You simply need to add the HTTP package by running this on command line in your project :

meteor add http

Also you need a call back using Meteor.call client side.

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. 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);});

本文标签: javascriptMeteorhttp method is undefined on serverStack Overflow