Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to obtain clientAddress? #50

Open
vmptk opened this issue Apr 30, 2016 · 7 comments
Open

How to obtain clientAddress? #50

vmptk opened this issue Apr 30, 2016 · 7 comments

Comments

@vmptk
Copy link

vmptk commented Apr 30, 2016

In the basic methods definitions you can fetch the client connection details via simple this.connection....

How to fetch the clientAddress when using the ValidatedMethod wrapper?

Regards,
Venelin

@stubailo
Copy link
Contributor

Should work the same way inside the 'run' method - can you post some code?

@vmptk
Copy link
Author

vmptk commented Apr 30, 2016

export const logMessage = new ValidatedMethod({
name: "logMessage",
validate: new SimpleSchema({
type: {type: String, optional: false},
message: {type: String, optional: false}
}).validator(),
run({type, message}){
let composedMessage = [${this.connection && this.connection.clientAddress || "127.0.0.1"}] ${message};
if (type === "ERROR") {
console.error(composedMessage);
} else {
console.log(composedMessage);
}
}
});

@stubailo
Copy link
Contributor

Weird, that should work. Let me add a test to make sure - will do today.

@vmptk
Copy link
Author

vmptk commented Apr 30, 2016

Thanks

@CaptainChemist
Copy link

Was there a resolution to this problem? I have a method that works fine normally, but the this.connection is undefined. Thanks so much!

export const insertEmail = new ValidatedMethod({
  name: 'emails.insert',
  validate: new SimpleSchema({
    email: { type: String }
  }).validator(),
  run(email) {
    console.log(email);
    console.log(this.connection);
    Emails.insert({email: email.email, ip: this.connection.clientAddress});
  },
});

@nabiltntn
Copy link
Contributor

@CaptainChemist @VenelinPetkov
this.connection works only in the server side.
so you need to include your run function code inside a : if (Meteor.isServer) { }

run({type, message}){
if (Meteor.isServer) { 
let composedMessage = [${this.connection && this.connection.clientAddress || "127.0.0.1"}] ${message};
if (type === "ERROR") {
console.error(composedMessage);
} else {
console.log(composedMessage);
}
}
}

You will loose Latency Compensation but i guess it's not important in your case.

Cheers

@stubailo
Copy link
Contributor

Hmm, any way to fix this? Seems like there's no way to get clientAddress on the client at all, so maybe there's nothing we can do about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants