This.connection is always undefined on server side

My code is as the following:

if (Meteor.isServer) {
  const geoip2 = require('geoip2');
  geoip2.init('/tmp/geoip2/GeoIP2-City.mmdb');


  Meteor.methods({
    'getGeoInfo'() {
      this.unblock();
      console.log(this.connection);
      const ip = this.connection.clientAddress;
      console.log(ip);
      return geoip2.lookupSimpleSync(ip);
    },
  });
}

It always prints undefined, why ?

I don’t know - that should work. What version of Meteor are you using?

Also, where are you calling your method from? If it’s called within the server code it will return null (so, not undefined).

I’m using Meteor 1.4.2.3, and I call this method from client, the client code is as follows:

componentDidMount() {
    Meteor.call('getGeoInfo', (error, result) => {
      if (error) {
        console.log(error);
      }  else {
        this.setState({address: result.city + ", " + result.subdivision + ", " +
          result.country + " " + result.postal});
      }
    })
  }

Now I have figured out why. Actually my code works if I deploy my web App to a server with a public IP, for example an AWS E2 instance with Elastic IP.

If I run it locally on my Macbook while development, then the connection is always null.

1 Like

Today I have the same problem. Thank you for sharing “solution”.