Reputation: 73
var fs = require('fs');
var path = require('path');
var walk = require('walk');
var xml2js = require('xml2js');
var jsonDir ;
var convertXml = function (xml_dir, json_dir) {
jsonDir = json_dir;
var walker = walk.walk(xml_dir, { followLinks: true });
walker.on("errors", fDirWalkError);
walker.on("end", fDirWalkEnd);
walker.on("file", fDirWalkFile);
}
function fDirWalkError (err) {
console.log ("fDirWalkError: " + err);
next (err);
}
function fDirWalkEnd () {
console.log ("======= End of directory walk");
}
function fDirWalkFile (root, fileStat, next) {
if (fileStat.name.indexOf(".xml") < 0) {
console.log ("skipping file " + fileStat.name + " (does not end in .xml)");
return;
} else {
var xml_file = path.resolve(root, fileStat.name);
console.log ("xml file: " + xml_file);
fs.readFile('xml_file', function (err, data) {
if (err) {
console.log ("error reading file:" + xml_file);
next (err);
}
xml2js.parseString (data, function (err, json_obj) {
if (err) {
console.log (err);
next (err);
}
var json_string = JSON.stringify(json_obj, null, 2);
var json_file = path.resolve (jsonDir, path.basename(xml_file).replace(/\.xml$/, ".json"));
console.log ("json file: ", json_file);
fs.writeFile(json_file, json_string, "utf8", function (err) {
if (err) {
console.log ("error converting yin (%s) to json(%s)", xml_file, json_file);
next (new Error ("error converting xml(" + xml_file + ") to json(" + json_file + ")"));
}
else {
console.log ("Converted xml (%s) to json(%s)", xml_file, json_file);
}
});
});
});
}
next ();
}
module.exports.convertXml = convertXml;
var path = require ('path');
var xml2js = require ('./xml2js');
console.log ("__dirname: " + __dirname);
var templateDir = path.resolve (__dirname);
var xmlDir = path.resolve (templateDir, "xml");
var jsonDir = path.resolve (templateDir, "jsons");
console.log( templateDir);
xml2js.convertXml (xmlDir, jsonDir)
/Users//Documents/GitHub//xml2js.js:19 next (err); ^
ReferenceError: next is not defined at Walker.fDirWalkError (/Users//Documents/GitHub//xml2js.js:19:9) at Walker.emit (events.js:321:20) at Walker._wPostFilesHandler (/UsersDocuments/GitHub//node_modules/walk/lib/walk.js:134:10) at /Users//Documents/GitHub//node_modules/foreachasync/forEachAsync.js:15:16 at Array.forEach () at Walker.next [as _wCurFileCallback] (/Users//Documents/GitHub/node_modules/foreachasync/forEachAsync.js:14:15) at Walker._wLstatHandler (/Users//Documents/GitHub//node_modules/walk/lib/walk.js:84:10) at /Users//Documents/GitHub//node_modules/walk/lib/walk.js:106:12
Upvotes: 1
Views: 189
Reputation: 91
depends on the walk documentation at https://www.npmjs.com/package/walk
the next param is the third param that the event send
in your case you are using
function fDirWalkError (err) {
console.log ("fDirWalkError: " + err);
next (err);
}
but you didnt get next function from the module in your function args
try to put it in your handler args
like this
function fDirWalkError (err, nodeStatsArray, next) {
console.log ("fDirWalkError: " + err);
next (err);
}
it should work
Upvotes: 1