shaunakde
shaunakde

Reputation: 3108

Send data from Server to Client in Meteor

I want to get the contents of a web-page, do some processing on the server side (to avoid running into same origin security problems on the client) and then send the result of that to the client for display.

I have written a Meteor Method to help get the data as:

Meteor.methods(
    {
        getURL: function(url_l){
            console.log("Request: "+url_l)

            //  var httpResult = HTTP.get(url_l){}
            //  return httpResult.data.response;

            var response = HTTP.get(url_l)
            var status = response.statusCode
            var content = response.content

            console.log(status)
            console.log(content)

            return content
        }
    });

The return unfortunately doesn't work.

Short of making database entries I cant think of a way of passing this data back to the client. I get a 'undefined' on the return.

Update

I also tried the async callback:

Meteor.methods(
    {
        getURL: function(url_l){
                console.log("Request: "+url_l)
            //  var httpResult = HTTP.get(url_l){}
            //   return httpResult.data.response;
            var response = HTTP.get(url_l, function(err,res){
                    if(err)
                        console.log(err)

                        var status = res.statusCode
                var content = res.content

              console.log(status)
              console.log(content)
              return content

            });

             }
    });

Just clarifying the question - regardless of if I use a sync or async HTTP callback, when I do this:

//AUtofetch
a = Meteor.call("getURL",url_l)
console.log(a)

I get an undefined.

The url they entered is: http://shaunak.de
siteace.js:115 undefined

Upvotes: 1

Views: 1612

Answers (2)

shaunakde
shaunakde

Reputation: 3108

After multiple attempts at solving this as suggested in the answers, I realized that it would be easier to simply return the entire HTTP Get function.

I defined a Meteor Method, getUrl with takes the request url as a parameter:

Meteor.methods(
    {
        'getURL': function(url_l){
                console.log("Request: "+url_l)
    return HTTP.get(url_l)
    } 

And I accessed it using this call:

Meteor.call("getURL",url_l,{},function(err,res){
         if(err){
           console.log('Error: '+err);
         }
         if(!err){
         console.log('Response: '+res);
            }
       });

This circumvents the problems that you face with Single Origin Policies in browsers successfully.

Upvotes: 0

Julien Leray
Julien Leray

Reputation: 1687

Are you sure you're get request is done before returning it?

Usually you have to set a callback to HTTP.get to be sure the request is done:

From the doc:

HTTP.get(url, [callOptions], [asyncCallback])

Try:

   getURL: function(url_l){
        console.log("Request: "+url_l)

        HTTP.get(url_l, function(err, response){
            if(err)
                console.log(err);
            var status = response.statusCode
            var content = response.content

            console.log(status)
            console.log(content)
            return content
        });
    }

Upvotes: 1

Related Questions