gadicc / meteor-headers

Access HTTP headers on both server and client. Client IP with proxy support.
https://atmospherejs.com/gadicohen/headers
GNU Lesser General Public License v3.0
61 stars 21 forks source link

methodClientIP - TypeError: undefined is not a function #27

Closed picsoung closed 9 years ago

picsoung commented 10 years ago

Hi,

I am trying to use the new methodClientIP in a Meteor.method but I got a error TypeError: undefined is not a function on the particular line


var ip = headers.methodClientIP(this);

But when I print ip I have the correct localhost IP. Anyway I could avoid this error ? thanks

gadicc commented 10 years ago

Mmm, weird there's an error and it still works :> What Meteor version are you using and where are you using this (method or publish?), and if you're inside an anonymous function, did you store self = this ?

Also since Meteor 0.7.1, you can use this.connection.clientAddress.

picsoung commented 10 years ago

I am using Meteor 0.8.1.1 (latest).

the code is

Meteor.methods({
  validateCaptcha: function(){
      var self = this;
      var ip = self.connection.clientAddress;
      //headers.methodClientIP(self);
      console.log("IP",ip);
  }
});

Both work but still throwing an error...

scorpwarp23 commented 10 years ago

I believe your error occurs cause the reference to this is lost inside the visualcaptcha method. Try this:

Meteor.methods({
  var self = this;
  validateCaptcha: function(){
      var ip = self.connection.clientAddress;
      //headers.methodClientIP(self);
      console.log("IP",ip);
  }
})
gadicc commented 10 years ago

Oops sorry, this thread fell through the cracks a bit.

@picsoung, hope you got this working by now, sorry :) But if you still got an error even with the self.connection.clientAddress line, then the error isn't from the headers package, because you aren't using it there. It's very weird, did you figure it out?

@scorpwarp23, Meteor.methods accepts a key-value dictionary... it's not passed a function and unfortunately you can't put JavaScript in the middle of the object declaration. I don't think the self scope was the problem though.