I've worked hard to solve a probelm where I can now parse a lot of xml into my js and create an object within the functions that contains all the data from the xml that I want.
What I can't do is get a function to return the object as a result so that I can use it in my wider code.
I've logged to console each exit point of each nested function and worked out where it gets stuck but can't work out how to get the data any further...
Could you have a look and see why it is stuck at the point where I've commented. Many thanks if you choose to help a little!
//now accessing an xml in a file structure
const path = nativeRequire('path')
const fs = nativeRequire('fs');
const xml2js = nativeRequire('xml2js');
const glob = nativeRequire('glob');
//const files = glob.sync(path.join(__dirname, '../../Steinberg/Cubase/Expression Maps/Native Instruments/Symphony Series/*.expressionmap'));
const mapFiles = glob.sync(path.join(__dirname, '../ExpressionMaps/*.expressionmap'));
var artArr = []
var artColor = []
var trackID = '117-4' //test value - this will come from the TRACK ID sysex stuff
buildMap() //I need Build Map to populate Objects artArr and artColor and return them so I can use them in other places in the code
function buildMap() {
mapFiles.forEach(function (mapFile) { //sends mapFile into the parser
//debug
//console.log('Selected Tracks ' + mapFile); // List out all the expression maps
//console.log('mapFiles length' + mapFiles.length) // gets the length of list of the files - number of expression maps in directory
//debug
var parser = new xml2js.Parser({ explicitArray: false, mergeAttrs: true }); // 'mergeAttrs: true' was essential
if (mapFile.includes(trackID)) {
console.log('Selected Map: ' + mapFile);
fs.readFile(mapFile, function (err, data) {
parser.parseString(data, function (err, result) {
let art = result.InstrumentMap.member[1].list.obj;
for (let i = 0, len = art.length; i < len; i++) {
console.log('Articulation at poition: ' + i + ' ' + art[i].member[1].string.value + '; ' + 'Color Value: ' + art[i].int.value) // articulatins and color value
artArr[i] = art[i].member[1].string.value
artColor[i] = art[i].int.value
}
});
//THE BELOW LOGS TO CONSOLE OK
console.log('CHECK LOG artArr[0] ' + artArr[0])
console.log('CHECK LOG artArr[1] ' + artArr[1])
console.log('CHECK LOG artArr[2] ' + artArr[2])
console.log('CHECK LOG artArr[3] ' + artArr[3])
console.log('CHECK LOG artArr[4] ' + artArr[4])
console.log('CHECK LOG artArr[5] ' + artArr[5])
console.log('CHECK LOG artArr[6] ' + artArr[6])
console.log('CHECK LOG artArr[7] ' + artArr[7])
});
//THE BELOW LOGS TO CONSOLE as UNDEFINED artArr and artColor get stuck here
console.log('CHECK LOG 2 artArr[0] ' + artArr[0])
console.log('CHECK LOG 2 artArr[1] ' + artArr[1])
console.log('CHECK LOG 2 artArr[2] ' + artArr[2])
console.log('CHECK LOG 2 artArr[3] ' + artArr[3])
console.log('CHECK LOG 2 artArr[4] ' + artArr[4])
console.log('CHECK LOG 2 artArr[5] ' + artArr[5])
console.log('CHECK LOG 2 artArr[6] ' + artArr[6])
console.log('CHECK LOG 2 artArr[7] ' + artArr[7])
}
});
}
console.log('Test log a value from artArr ' + artArr[3]) //Needs to return a value but currently returns 'undefined'
console.log('Test log a value from artColor ' + artColor[3]) //Needs to return a value but currently returns 'undefined'