【问题标题】:Opening maxmind DB and accessing it in nodejs打开 maxmind 数据库并在 nodejs 中访问它
【发布时间】:2019-08-16 08:56:19
【问题描述】:

以前我是这样使用的:

Opening Maxmind db in Nodejs

现在,按照node 10 更新了模块。 所以,需要帮助来整合它。

reference

const maxmind = require('maxmind');
exports.getIsoCountry = function(pIpAddress) {

  modules.debugLog('inside getIsoCountry : ',pIpAddress);

  maxmind.open(sGlAppVariable.maxmindDbPath)
  .then(function(lookup) {
    var ipData = lookup.get(pIpAddress);
    //console.log(ipData);
    console.log('iso_code',ipData.country.iso_code);
    return ipData.country.iso_code;
  });

}

console.log(getIsoCountry('66.6.44.4')); 它应该打印国家代码。但它总是undefined。因为这是一个承诺。

如何调用这个getIsoCountry函数?

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript node.js promise maxmind


    【解决方案1】:

    您需要等待执行完成,为此,您应该使用Promise

    修改你的代码如下,然后它应该可以工作:

    const maxmind = require('maxmind');
    exports.getIsoCountry = function(pIpAddress) {
      return new Promise((resolve, reject) => {
        modules.debugLog('inside getIsoCountry : ',pIpAddress);
          maxmind.open(sGlAppVariable.maxmindDbPath)
          .then(function(lookup) {
            var ipData = lookup.get(pIpAddress);
            console.log('iso_code',ipData.country.iso_code);
            resolve(ipData.country.iso_code);
          });
      });
    }
    
    getIsoCountry("66.6.44.4").then((rData) => {
      console.log(rData)
    });
    

    以下是示例代码:

    var getIsoCountry = function(pIpAddress) {
    
      return maxmind().then(function() {
           return "Code for IP: " + pIpAddress;
        });
    
      function maxmind() {
        return new Promise((resolve, reject) => {
          resolve("done")
        });
      }
    
    }
    
    getIsoCountry("1.1.1.1").then((data) => {
      console.log(data)
    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多