// on server
Meteor.methods({
'getIP': function () {
return this.connection.clientAddress;
}
});
// on client
Meteor.startup(function() {
Meteor.call('getIP', function (err, res) {
console.log(res); // returns undefined
});
});
进一步挖掘,服务器上的“this.connection”似乎只有3个东西:id、close、onClose,但没有clientAddress。我错过了什么?
其他信息:
- 正在运行 vagrant在 Windows 主机上 ubuntu远程。
- 我尝试将环境变量 HTTP_FORWARDED_COUNT 设置为 0, 1 和 2 但没有运气。
- 文档引用:http://docs.meteor.com/#publish_connection
最佳答案
我正在使用 Meteor 0.9.3.1 并在 Meteor 方法中调用 this.connection.clientAddress
对我来说很有效。我将客户端 IP 地址作为字段存储在我正在使用的集合集中。
我确实遇到了一个问题,客户端尝试模拟调用并在模拟调用效果时抛出异常... this.connection is undefined 错误。但我注意到我的文档已使用正确的客户端 IP 地址正确存储。因此解决方法就是不要让客户端尝试模拟这一点。
在我的 Meteor 方法中,我刚刚添加了一个 Meteor.isServer
检查。如果客户端模拟客户端 IP 为 0.0.0.0
也没关系:
var ip = "0.0.0.0";
if(Meteor.isServer) {
if(!this.connection.clientAddress)
throw new Meteor.Error(403, "Server Error: You must be connected.");
else
ip = this.connection.clientAddress;
}
// ...store document with ip...
一切都按预期进行。
关于javascript - 在 Meteor Method 中,为什么 this.connection.clientAddress 未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24032129/