I have this simple code:
File app.js:
var cheerio = require('cheerio');
var express = require('express');
var fs = require('fs');
var request = require('request');
var downloader = require('./routers/downloader.js');
const app = express();
downloadAll();
function downloadAll() {
downloader.test();
}
File downloader.js:
var cheerio = require('cheerio');
var express = require('express');
var fs = require('fs');
var request = require('request');
var methods = {};
methods.test = function(req, res) {
for(var i = 1; 10; i++) {
console.log(i); // DOESN'T WORK
}
var obj;
fs.readFile('./output/ita-2015-24m.json', 'utf8', function(err, data) {
if(err) {
throw err;
}
obj = JSON.parse(data);
console.log(obj);
}
}
module.exports = methods;
When I run app.js using node app.js, I get: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ....
The loop doesn't end.
I try to change position to this loop, interting it inside readFile function but it's the same problem.
My goal is iterate throug obj object.
Why? How can I solve?
var methods = {};is a variable that hold the value of an emptyobject. You should use afor inloop (watch out for non-enumerable properties) instead of testing your code with a for loop