以前我用的是这样的:
现在,按照node 10更新模块。所以,需要帮助来整合它。
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函数?
任何帮助都将不胜感激。
发布于 2019-08-16 12:19:49
您需要等待执行完成,为此,您应该使用承诺。
修改您的代码如下,然后它应该工作:
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)
});
https://stackoverflow.com/questions/57521672
复制相似问题