繁体   English   中英

流星:无法存储来自外部api的数据

[英]METEOR: Fail to store data from external api

我需要存储注册用户的IP数据。 插入新的注册用户后,我正在使用telize api检索IP和collection hooks以进行更新。

服务器/methods.js

Meteor.methods({
  // The method expects a valid IPv4 address
  'geoIp': function () {
    // Construct the API URL
    this.unblock();
    var apiUrl = 'http://www.telize.com/geoip/';
    // query the API
    var response = HTTP.get(apiUrl);
    Meteor.users.update(
          {_id: this.userId}, 
          {$set: {ipdata: response}}
      )
  }
});

lib / schemas.js

  ipdata: {
    type: Object,
    optional: true
  }

服务器/hooks.js

Meteor.users.after.insert(function (userId, doc) {
  Meteor.call('geoIp');
});

这些代码不会导致错误,可以成功注册新用户,但是无法存储ipdata

有人发现我的代码有什么问题吗?

非常感谢你..

我很确定在这种情况下未设置this.userId 您是在服务器端而不是从另一个方法的主体中调用Meteor.call() ,因此被调用的方法没有上下文。

相反,您可能只想为此使用一个常规函数。 例如

addGeoIp = function(userId, ip){
    var response = HTTP.get(apiUrl);
    Meteor.users.update(userId, {$set: {ipdata: response}});
}

Meteor.users.after.insert(function (userId, doc) {
    addGeoIp(userId, ip);
});

而且,正如Mark所说, http://www.telize.com/geoip/本身只会返回服务器的IP,而不是客户的IP。 因此,您需要将客户端的IP传递到API中以获取位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM