fighting for 5 days with the problem. I have 3 files, needed to share one varible kvmIndex between them
getKvmIndex.js
var kvmIndex=[],
exports.kvmIndex = kvmIndex;
exports.getSNMP = function (callback) {
async.each(switchIps, function(switchIp, callback1) {
goGo(switchIp, callback1);
}, function(err) {
callback();
})
};
match.js
var app1 = require("./app.js");
var kvmIndex;
exports.kvmIndex = kvmIndex;
...
exports.matchAll = function(callback) {
async.series([
function(callback) {
kvmIndex = app1.kvmIndex;
decToHex(callback);
},
function(callback){
matchSt('getPortToSt2', callback);
},
function(callback){
console.log(kvmIndex); //Here it defined! Works good.
callback()
}
])
callback();
}
app.js
var kvmSNMP = require('./getKvmIndex')
, match = require('./match')
, async = require('async')
, kvmIndex = [];
...
async.series([
function(callback) {
kvmSNMP.getSNMP(callback);
},
function(callback) {
exports.kvmIndex = kvmSNMP.kvmIndex;
callback();
},
function(callback) {
match.matchAll(callback);
},
function (callback) {
kvmIndex = match.kvmIndex;
callback();
},
function (callback) {
console.log(match.kvmIndex); //Doesnt work(
callback();
}
])
What i'm doing:
In getKvmIndex.js it works fine, but in match.js no. Can anybody help me?
You need to define kvmIndex
in one module (file) and one only. It that module, add to it exports
like you do. The other modules (files) need to require
the module defining kvmIndex
, and use it by accessing myModule.kvmIndex
.
Notes:
1) It would be nicer to export getKvmIndex()
from getKvmIndex.js
, and not the variable kvmIndex
directly.
2) You may have a bug in match.js
-- the callback is called immediately. Did you maybe mean to pass callback
and the 2nd argument to async.series()
?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.