Hi,
Sorry about that, I’ve just realized I gave you an incomplete solution…
So, you’re returning something in the function processFile(), that’s good.
However, where your code is processFile(content)
, you’re not doing anything with the value that was returned. So, I suggested you put return processFile(content)
there.
However, this also doesn’t work, because then the value is returned to nothing again:
fs.readFile(filePath, 'utf8', function (err, data) {
Look carefully up there - you’re creating an anonymous function - that’s why the returned value is discarded.
This way of writing Javascript code is no longer recommended, as you can see it’s very easy to make mistakes like this. Nowadays, we use async/await:
Here’s my suggestion:
const { XMLParser } = require('fast-xml-parser');
const fs = require('fs');
var util = require('util');
exports.GetJsonXml = async function (options) {
function processFile(content) {
const options = {
ignoreAttributes: false
};
const parser = new XMLParser(options);
const data = parser.parse(content);
const myJSON = JSON.stringify(data);
var objectValue = JSON.parse(myJSON);
console.log(objectValue.offer.products.product.sizes);
return objectValue.offer.products.product.sizes
}
var filePath = __dirname + '/../../../' + options.path;
const data = await fs.promises.readFile(filePath, 'utf8');
return processFile(data)
};
Edit: Ensure you’re using at least NodeJS 16