简体   繁体   English

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

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

I need to store IP data of registered user. 我需要存储注册用户的IP数据。 I'm using telize api for retrieving IP and collection hooks for updating after inserting new registered user. 插入新的注册用户后,我正在使用telize api检索IP和collection hooks以进行更新。

server/methods.js 服务器/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 lib / schemas.js

  ipdata: {
    type: Object,
    optional: true
  }

server/hooks.js 服务器/hooks.js

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

These code resulting no error, successfully registering new user, but fail to store ipdata 这些代码不会导致错误,可以成功注册新用户,但是无法存储ipdata

Anyone have figured out what's wrong with my code? 有人发现我的代码有什么问题吗?

thank You very much.. 非常感谢你..

I'm pretty sure this.userId is not set in this case. 我很确定在这种情况下未设置this.userId You're calling Meteor.call() server-side and not from another method's body, so the called method has no context. 您是在服务器端而不是从另一个方法的主体中调用Meteor.call() ,因此被调用的方法没有上下文。

Instead, you'd probably want to just have a regular function for this. 相反,您可能只想为此使用一个常规函数。 Eg 例如

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);
});

Also, as Mark said, http://www.telize.com/geoip/ on its own would just return the server's IP, not the client's IP. 而且,正如Mark所说, http://www.telize.com/geoip/本身只会返回服务器的IP,而不是客户的IP。 So you need to pass the client's IP into the API to get the location. 因此,您需要将客户端的IP传递到API中以获取位置。

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

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