我想从全局函数声明一个数组,以便我的其他函数可以使用它,但我不知道如何,因为我使用一个csvtojson转换器,使整个事情很长,并想知道如果这是申报还是不申请?如何从全局函数声明一个数组?
JS:
//require the csvtojson converter class
var Converter = require("csvtojson").Converter;
// create a new converter object
var converter = new Converter({});
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/myproject';
// call the fromFile function which takes in the path to your
// csv file as well as a callback function
var JSarray = converter.fromFile("./NTA-SAM-Inventory-List-Security-
Management-New_2017.csv",function(err, result, callback){
if(err){
console.log("An Error Has Occured");
console.log(err);
}
// the result of the conversion
console.log(result);
result.toArray(function(err,doc){
if(err) throw err;
console.log('ohhhhh');
return callback(null, doc);
}
});
var array=[JSarray(function(err,doc)]
这是我的声明数组。 我的数组是doc,所以我可以返回回调函数,但是我应该如何获得数组,考虑到我的converter.fromFile("./NTA-SAM-Inventory-List-Security- Management-New_2017.csv"
太长,所以在声明数组时我忽略了它,或者我做错了?谢谢。
更新 只是想澄清一下,如果我做得对。
var JSarray = converter.fromFile("./NTA-SAM-Inventory-List-Security-M
anagement-New_2017.csv",function(err, result, callback){
// if an error has occured then handle it
if(err){
console.log("An Error Has Occured");
console.log(err);
}
// the result of the conversion
console.log(result);
result.toArray(function(err,doc){
if(err) throw err;
console.log('ohhhhh');
return callback(null, doc);
var myArray= doc;
GLOBAL.GlobalMyArray = myArray;
});
});
这是否正确跟随您的答案在全球范围内声明?
全局变量是不好的做法。 –
更改为'var myArray = doc; GLOBAL.GlobalMyArray = myArray;返回回调(null,doc);' –