I have a for
loop within an https
request -
for (var i = 0; i < folder.length; i++) {
var taskCount = taskGetter(folder[i].id);
folders[i] = {"title":folder[i].title, "id":folder[i].id, "taskCount":taskCount};
}
printFolderInfo(folder);
var taskCount
calls the method taskGetter()
which returns a value and populates folders[i]
and the method printFolderInfo()
writes them to a json
file -
var taskGetter = function(id) {
var taskCount = 0;
options.path = '/api/v3/folders/' + id +'/tasks';
var req = https.request(options, function(res) {
var body = [];
res.on('data', function(data) {
body.push(data);
})
.on('end', function() {
body = Buffer.concat(body);
var task = JSON.parse(body.toString()).data;
taskCount = task.length;
return taskCount;
})
});
req.end();
}
The problem is that taskCount
keeps returning undefined
. When I do console.log(taskCount)
inside taskGetter()
though, it prints out the values fine. It seems like the for
loop is moving on to folders[i]
before taskCount
gets the value from taskGetter()
.
Is there something that I'm doing wrong here? Any input is appreciated!
via Anish Sana
No comments:
Post a Comment