Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when calling http method from client in meteor

Tags:

meteor

I am trying to consume a REST API in the meteor application. Inside the server.js file which is in the server folder, I have written this code:

 Meteor.methods({
        checkTwitter: function () {
            this.unblock();
            return Meteor.http.call("GET", "http://search.twitter.com/search.json?q=perkytweets");
        }
    });

Inside the client.js file, which is in the client folder, I have written down this code:

  Meteor.call("checkTwitter", function(error, results) {
        console.log(results.content); //results.data should be a JSON object
    });

I get this error message in console: "Exception while simulating the effect of invoking 'checkTwitter' Error: Can't make a blocking HTTP call from the client; callback required". I have the callback function defined in client due to which I don't understand this error. What is it that I am doing wrong?

like image 521
Deep Arora Avatar asked Jul 23 '26 21:07

Deep Arora


2 Answers

Meteor.http has been deprecated, please see the HTTP package.

like image 185
1321941 Avatar answered Jul 26 '26 16:07

1321941


I think that since there's a stub, "checkTwitter" will actually also run on the client. Once the server returns, its result will overwrite the result from teh client run. In this case, since Meteor.http.call can't run on the client without a callback, you get the error.

Try changing:

Meteor.methods({
         checkTwitter: function () {
             this.unblock();
             return Meteor.http.call("GET", "http://search.twitter.com/search.json?q=perkytweets");
         }
     });

With

    Meteor.methods({
            checkTwitter: function () {
              if (Meteor.isServer) {
                this.unblock();
                return Meteor.http.call("GET", "http://search.twitter.com/search.json?q=perkytweets");
              }
            }
        });
like image 30
rivarolle Avatar answered Jul 26 '26 14:07

rivarolle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!