【问题标题】:get Client IP in Accounts.OnCreateUser Meteor JS在 Accounts.OnCreateUser Meteor JS 中获取客户端 IP
【发布时间】:2017-05-17 20:23:27
【问题描述】:

我正在尝试获取客户端 IP 地址

Accounts.onCreateUser((options, user) => {

  var data = {
      "updated_at" : new Date(),
      "registration_ip" : user.connection.clientAddress,
      "recruiter" : "SELF",
      "registration_time" : new Date(),
  };

  _.extend(user.profile, data);
  return user;
});

我得到错误:

I20170511-19:11:26.792(5.5)? Exception while invoking method 'createUser' 
TypeError: Cannot read property 'clientAddress' of undefined

如何在此处获取客户端 IP 地址?

【问题讨论】:

  • user.connection 是否返回任何内容?另外,当您尝试this.connection 时会发生什么?
  • @blueren:感谢您的回复。它返回“未定义”。
  • 也许你必须在用户登录时检索它

标签: meteor meteor-accounts


【解决方案1】:

这是我用来获取用户 IP 地址的代码。

Accounts.validateLoginAttempt(function (attempt) {
    /* Before any checks save user first time login attempt IP address */
    let IPAddress = Global.getNestedValue(attempt, 'connection.httpHeaders.x-forwarded-for');

    if (IPAddress && attempt.user && !attempt.user.firstTimeIP) {
        Meteor.users.update({ _id: attempt.user._id }, { $set: { firstTimeIP: IPAddress } })
    }
}

【讨论】:

  • 非常感谢,我会尽力让您知道的。
猜你喜欢
  • 1970-01-01
  • 2016-10-07
  • 2015-06-06
  • 2020-11-10
  • 2014-12-01
  • 2020-12-08
  • 1970-01-01
  • 2017-08-11
  • 1970-01-01
相关资源
最近更新 更多