MY issue is parse large xml file (with xml2js), and forEach element in cycle(product) , download image and save it to file. I wrote this code:
var fs = require('fs'); var request = require('request'); var parseString = require('xml2js').parseString; var baseUrl = 'http://shop.nag.ru/uploads/catalog_item_image_main/'; var async = require('async'); var processImg = require('./downloader'); var q = require('q'); var readFileSync = function (){ var xml = fs.readFileSync("./test.xml", "utf8"); return xml; }; readFileSync.then(function(xml) { parseString(xml, function (err, result) { if(err)return error; return result.product_list.product; }) }) .then(function(products){ products.forEach(function(prdt) { }); }).catch(function (err) { console.log(err); }); But after run I got this error :
readFileSync.then(function(xml) { ^ TypeError: undefined is not a function at Object.<anonymous> (D:\WorkVrp\nodeImageParser\processing.js:19:14) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Function.Module.runMain (module.js:501:10) at startup (node.js:129:16) at node.js:814:3
readFileSyncreturns a value and not a promise, you're returning fromparseStringwhich doesn't make sense since it takes a callback (vs returns a promise). Look into how to convert callback APIs to promises.readFileSyncdoes return the file contents, not a promise?!asyncandq, but using neither?