This.connection is always undefined on server side


#1

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 ?


#2

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


#3

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

#4

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.


#5

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