Saturday 8 April 2017

failing to get output from function

I'm trying to figure out how to get value from xml file and output that value to console.

const path = require('path');
const fs = require('fs');
const parseString = require('xml2js').parseString;

const sourceFile = path.join(__dirname, 'books.xml');

const document = (sourceFile, callback) => {
  fs.readFile(sourceFile, { encoding: 'utf-8' }, function (err, data) {
    if (err) {
      return err;
    }

    parseString(data, (err, result) => {
      result.catalog.book.forEach(users => {
        callback(users.email);
      });
    });
  });
};

console.log(document(sourceFile, data => data));

The xml itself is very simple:

<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <email>test@test.com</email>
   </book>
</catalog>

But what I am getting from the function is undefined. Why does it happen?



via Karolis Arbačiauskas

No comments:

Post a Comment