Node JS 如何从函数的回调函数访问全局变量?
Node JS How to access global variable from Callback function of a function?
我正在使用 NodeJS 进行事件捕获。所有调用都将转到 getme 函数。从这个函数中,我调用了 getUserLocation() 函数,该函数 return 基于 ip 的地理定位。如何根据这些值更新全局变量?
var getClientAddress = function (req) {
return (req.get('x-forwarded-for') || '').split(',')[0] || req.connection.remoteAddress;
}
var getClientLocation = function (ipaddress, callback) {
freegeoip.getLocation(ipaddress, function(err, location) {
if (err) throw err;
return callback(location);
});
}
var store = [{'hello': 'world', 'country': 'India', 'City': 'Indupur'}];
for (eve=0;eve<store.length;eve++){
if(!store[eve].lat){
clientIp = getClientAddress(req);
getClientLocation("XXX:XX:XX:XXX", function(resp) {
console.log(resp);
store[eve].country = store[eve].country || resp.country_name;
store[eve].region = store[eve].region || resp.region_name;
store[eve].city = store[eve].city || resp.city;
store[eve].lat = store[eve].lat || resp.latitude;
store[eve].lng = store[eve].lng || resp.longitude;
});
}
但是商店无法访问。它是未定义的。如何更新商店?
实际代码:
这是实际的代码:
https://github.com/Gowtham95india/CapVengine/blob/master/server.js
错误信息如下:
Server started! At http://localhost:8080
Redis started! Ready to perform
{ e: '[{"device_id":"dsfkdjf-dsfdls-fejfskj-e2oiej2j3jf","user_id":2124,"email":"gowtham95india@gmail.com","event_properties":{"utm_source":"HelloWorld"}, "lat":""}]',
v: 2 }
2017-02-11T09:02:10.838Z
{ ip: '121.244.122.142',
country_code: 'IN',
country_name: 'India',
region_code: 'MH',
region_name: 'Maharashtra',
city: 'Phursungi',
zip_code: '412308',
time_zone: 'Asia/Kolkata',
latitude: 18.4667,
longitude: 73.9833,
metro_code: 0 }
/Users/GowthamSai/Documents/repo/capeve/server.js:111
store[eve].country = store[eve].country || resp.country_name;
^
TypeError: Cannot read property 'country' of undefined
at /Users/GowthamSai/Documents/repo/capeve/server.js:111:48
at /Users/GowthamSai/Documents/repo/capeve/server.js:36:16
at Request._callback (/Users/GowthamSai/Documents/repo/capeve/node_modules/node-freegeoip/lib/freegeoip.js:25:16)
at Request.self.callback (/Users/GowthamSai/Documents/repo/capeve/node_modules/request/request.js:187:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/Users/GowthamSai/Documents/repo/capeve/node_modules/request/request.js:1048:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/Users/GowthamSai/Documents/repo/capeve/node_modules/request/request.js:969:12)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
您的问题不在于访问 store
。该全局变量已定义。
您的问题是访问 store[eve]
,因为 您从未定义 。
您直接尝试读取 store[eve].lat
,而无需将任何内容分配给 store[eve]
(例如使用 store[eve] = store[eve] || {}
)。
您还有其他一些问题,它们是该问题的根本原因,解释如下:
- How do I return the response from an asynchronous call?
- JavaScript closure inside loops – simple practical example
我正在使用 NodeJS 进行事件捕获。所有调用都将转到 getme 函数。从这个函数中,我调用了 getUserLocation() 函数,该函数 return 基于 ip 的地理定位。如何根据这些值更新全局变量?
var getClientAddress = function (req) {
return (req.get('x-forwarded-for') || '').split(',')[0] || req.connection.remoteAddress;
}
var getClientLocation = function (ipaddress, callback) {
freegeoip.getLocation(ipaddress, function(err, location) {
if (err) throw err;
return callback(location);
});
}
var store = [{'hello': 'world', 'country': 'India', 'City': 'Indupur'}];
for (eve=0;eve<store.length;eve++){
if(!store[eve].lat){
clientIp = getClientAddress(req);
getClientLocation("XXX:XX:XX:XXX", function(resp) {
console.log(resp);
store[eve].country = store[eve].country || resp.country_name;
store[eve].region = store[eve].region || resp.region_name;
store[eve].city = store[eve].city || resp.city;
store[eve].lat = store[eve].lat || resp.latitude;
store[eve].lng = store[eve].lng || resp.longitude;
});
}
但是商店无法访问。它是未定义的。如何更新商店?
实际代码: 这是实际的代码:
https://github.com/Gowtham95india/CapVengine/blob/master/server.js
错误信息如下:
Server started! At http://localhost:8080
Redis started! Ready to perform
{ e: '[{"device_id":"dsfkdjf-dsfdls-fejfskj-e2oiej2j3jf","user_id":2124,"email":"gowtham95india@gmail.com","event_properties":{"utm_source":"HelloWorld"}, "lat":""}]',
v: 2 }
2017-02-11T09:02:10.838Z
{ ip: '121.244.122.142',
country_code: 'IN',
country_name: 'India',
region_code: 'MH',
region_name: 'Maharashtra',
city: 'Phursungi',
zip_code: '412308',
time_zone: 'Asia/Kolkata',
latitude: 18.4667,
longitude: 73.9833,
metro_code: 0 }
/Users/GowthamSai/Documents/repo/capeve/server.js:111
store[eve].country = store[eve].country || resp.country_name;
^
TypeError: Cannot read property 'country' of undefined
at /Users/GowthamSai/Documents/repo/capeve/server.js:111:48
at /Users/GowthamSai/Documents/repo/capeve/server.js:36:16
at Request._callback (/Users/GowthamSai/Documents/repo/capeve/node_modules/node-freegeoip/lib/freegeoip.js:25:16)
at Request.self.callback (/Users/GowthamSai/Documents/repo/capeve/node_modules/request/request.js:187:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/Users/GowthamSai/Documents/repo/capeve/node_modules/request/request.js:1048:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/Users/GowthamSai/Documents/repo/capeve/node_modules/request/request.js:969:12)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
您的问题不在于访问 store
。该全局变量已定义。
您的问题是访问 store[eve]
,因为 您从未定义 。
您直接尝试读取 store[eve].lat
,而无需将任何内容分配给 store[eve]
(例如使用 store[eve] = store[eve] || {}
)。
您还有其他一些问题,它们是该问题的根本原因,解释如下:
- How do I return the response from an asynchronous call?
- JavaScript closure inside loops – simple practical example