async.each confusion in NodeJS - node.js

I'm taking my first steps in NodeJS, and I'm having an issue with the async-module. I had the following code which works fine:
var http = require('http');
var fs = require('fs');
var async = require('async');
function load_albums(callback) {
fs.readdir("albums", function(err, content) {
console.log(content);
if(err) {
callback(err);
return;
}
var directories = [];
(function iterator(index) {
if(index == content.length) {
callback(null, directories);
return;
}
fs.stat("albums/" + content[index], function(err, stats) {
if (err) {
callback(err);
}
if(stats.isDirectory()) {
directories.push(content[index]);
}
console.log(index);
iterator(index + 1);
});
})(0);
});
}
function handle_request(request, response) {
load_albums(function(err, albums) {
if (err) {
response.writeHead(503, {"Content-Type": "application/json"});
response.end(JSON.stringify(err) + "\n");
return;
}
var out = { error: null,
data: { albums: albums}};
response.writeHead(200, { "Content-Type" : "application/json" });
response.end(JSON.stringify(out) + "\n");
});
}
var s = http.createServer(handle_request);
s.listen(8080);
This works fine, and gives the expected output:
{"error":null,"data":{"albums":["testdir1","testdir2"]}}
However, I intended to replace the iterator with the async.each function.
I ended up with this:
function load_albums(callback) {
fs.readdir("albums", function(err, content) {
console.log(content);
if(err) {
callback(err);
return;
}
var directories = [];
async.each(content, function(item, callback2) {
fs.stat("albums/" + item, function(err,stats) {
if(stats.isDirectory()) {
directories.push(item);
}
callback2();
});
});
callback(null, directories);
});
}
However, this doesn't seem to work, as "albums" seems to be empty now:
{"error":null,"data":{"albums":[]}}
What am I missing here? I guess it has something to do with calling the fs.stats() function, but I'm unsure about what I'm doing wrong.

async.each() takes three arguments. You are not passing the last one which is the one that tells you when it is done. You also haven't implemented error handling on fs.stat(). You can change to this:
function load_albums(callback) {
fs.readdir("albums", function(err, content) {
console.log(content);
if(err) {
callback(err);
return;
}
var directories = [];
async.each(content, function(item, callback2) {
fs.stat("albums/" + item, function(err,stats) {
if (!err && stats.isDirectory()) {
directories.push(item);
}
callback2(err);
});
}, function(err) {
callback(err, directories);
});
});
}

As answered by #jfriend00 final callback is third parameter of asyn.each. Currently this callback is running without waiting for async.each to complete.
Also you're serving albums for all the request. They should be served on a particular resource URL like /albums or /albums/
I have made these modifications to the code, now it loads albums on http://localhost:8080/albums otherwise it returns 'No Content.
var http = require('http');
var fs = require('fs');
var async = require('async');
function load_albums(loadCompleteCallback) {
fs.readdir("albums", function(err, content) {
console.log(content);
if(err) {
callback(err);
return;
}
var directories = [];
async.each(content, function(item, doneCallback) {
fs.stat("albums/" + item, function(err,stats) {
if(stats.isDirectory()) {
directories.push(item);
}
return doneCallback(err);
});
}
, function (err) {
loadCompleteCallback(err, directories);
});
});
}
function handle_request(request, response) {
console.log("requested path: " + request.url);
if(request.url.match(/^\/albums[\/]?$/) ) {
load_albums(function(err, albums) {
if (err) {
response.writeHead(503, {"Content-Type": "application/json"});
response.end(JSON.stringify(err) + "\n");
return;
}
var out = { error: null,
data: { albums: albums}};
response.writeHead(200, { "Content-Type" : "application/json" });
response.end(JSON.stringify(out) + "\n");
});
} else {
response.writeHead(200, { "Content-Type" : "application/json" });
response.end("No Content\n");
}
}
var s = http.createServer(handle_request);
s.listen(8080);
console.log("server running at : http://localhost:" + 8080);

Related

how to get the data from http.get

I have created a NodeJS server and created promises for the HTTP.get method and calling the get method function in created server but it showing error options.uri
var http = require('http');
var request = require('request');
var rese = null;
function initialize() {
var options = {
host: 'httpbin.org',
path: '/json',
};
return new Promise(function (resolve, reject) {
request.get(options, function (err, res, body) {
if (err) {
reject(err);
} else {
resolve(JOSN.parse(body));
}
/** res.on("data", function(chunk) {
//console.log("BODY: " + chunk);
result=chunk;
});*/
})
})
}
http.createServer(function (req, res) {
if (req.url == '/my') {
/**result=res.on("data", function(chunk) {
console.log("BODY: " + chunk);
});*/
var initializePromise = initialize();
initializePromise.then(function (res) {
rese = result;
console.log("Initialized user details");
// Use user details from here
console.log(userDetails)
}, function (err) {
console.log(err);
});
//res.end(result);
}
else {
res.end('please find the correct path');
}
}).listen(2000);
error:options.uri is a required argument
Your get method should include uri, The request api get call structure is as follows,
request.get("http://bin.org/json", options, function (err, res, body) {
if (err) {
reject(err);
} else {
resolve(JSON.parse(body));
}
/** res.on("data", function(chunk) {
//console.log("BODY: " + chunk);
result=chunk;
});*/
})
Try to make this changes, it will work

TypeError: callback is not a function in nodejs

I am trying learn nodejs and stumble upon this error
TypeError: callback is not a function
when I am trying to call the server using this command.
curl http://localhost:8090/albums.json
and here is the code for my server.js
var http = require('http'),
fs = require('fs');
function load_album(album_name, callback) {
fs.readdir("albums/", +album_name, (err, files) => {
if (err) {
if (err.code == "ENOENT") {
callback(make_error("no_such_album", "That album doesn't exist"));
} else {
callback(make_error("can't load album", "The server is broken"));
}
} else {
//callback(null, files);
var only_files = [];
var path = 'albums/${album_name}/';
var iterator = (index) => {
if (index == files.length) {
var obj = {
short_name: album_name,
photos: only_files
};
callback(null, obj);
return;
}
fs.stat(path + files[index], (err, stats) => {
if (!err && stats.isFile()) {
only_files.push(files[index]);
}
iterator(index + 1);
});
};
iterator(0);
}
});
}
function handle_incoming_request(req, res) {
console.log("incoming request: " + req.method + " " + req.url);
if (req.url == '/albums.json') {
load_album((err, albums) => {
if (err) {
res.writeHead(500, {
"Content-Type": "application/json "
});
res.end(JSON.stringify({
code: "cant_load_albums",
message: err.message
}));
} else {
var output = {
error: null,
data: {
albums: albums
}
};
res.writeHead(200, {
"Content-Type": "application/json"
});
res.end(JSON.stringify(output) + "\n");
}
});
} else if (req.url.substr(0, 7) == '/albums' && req.url.substr(req.url.length - 5) == '.json') {
//user is requesting contents of album
load_album(req.url.substr(7, req.url.length - 12), (err, photos) => {
if (err) {
res.writeHead(500, {
"Content-type": "application/json"
});
res.end(JSON.stringify(err));
} else {
var output = {
error: null,
data: {
photos: photos
}
};
res.writeHead(200, {
"Content-Type": application / json
});
res.end(JSON.stringify(output) + "\n");
}
});
} else {
res.writeHead(404, {
"Content-type": "application/json"
});
res.end(JSON.stringify({
code: "no_such_page",
message: "No such page"
}));
}
}
var s = http.createServer(handle_incoming_request);
s.listen(8090);
can you tell me what's wrong with my code that I got an error telling me callback isn't a function?
thanks though
for more formatted code then you can go here https://jsfiddle.net/02dbx6m9/
var http = require('http'),
fs = require('fs');
function load_album(album_name, callback) {
fs.readdir("albums/", +album_name, (err, files) => {
if (err) {
if (err.code == "ENOENT") {
callback(make_error("no_such_album", "That album doesn't exist"));
} else {
callback(make_error("can't load album", "The server is broken"));
}
} else {
//callback(null, files);
var only_files = [];
var path = 'albums/${album_name}/';
var iterator = (index) => {
if (index == files.length) {
var obj = {
short_name: album_name,
photos: only_files
};
callback(null, obj);
return;
}
fs.stat(path + files[index], (err, stats) => {
if (!err && stats.isFile()) {
only_files.push(files[index]);
}
iterator(index + 1);
});
};
iterator(0);
}
});
}
function handle_incoming_request(req, res) {
console.log("incoming request: " + req.method + " " + req.url);
if (req.url == '/albums.json') {
load_album("ALBUM NAME", (err, albums) => {
if (err) {
res.writeHead(500, {
"Content-Type": "application/json "
});
res.end(JSON.stringify({
code: "cant_load_albums",
message: err.message
}));
} else {
var output = {
error: null,
data: {
albums: albums
}
};
res.writeHead(200, {
"Content-Type": "application/json"
});
res.end(JSON.stringify(output) + "\n");
}
});
} else if (req.url.substr(0, 7) == '/albums' && req.url.substr(req.url.length - 5) == '.json') {
//user is requesting contents of album
load_album("Album Name", req.url.substr(7, req.url.length - 12), (err, photos) => {
if (err) {
res.writeHead(500, {
"Content-type": "application/json"
});
res.end(JSON.stringify(err));
} else {
var output = {
error: null,
data: {
photos: photos
}
};
res.writeHead(200, {
"Content-Type": application / json
});
res.end(JSON.stringify(output) + "\n");
}
});
} else {
res.writeHead(404, {
"Content-type": "application/json"
});
res.end(JSON.stringify({
code: "no_such_page",
message: "No such page"
}));
}
}
var s = http.createServer(handle_incoming_request);
s.listen(8090);
You forgot to pass album name parameter in load_album method. That's why album_name parameter is assigned the actual callback, while callback parameter remains undefined.
Here is the root cause of your issue:
load_album((err, albums) => {
// ...
});
The signature for the function requires two parameters, yet you're only passing the first one:
function load_album(album_name, callback) {}
Therein, once called, callback will be undefined, yet you're trying to treat it as a callable. Here's a more succint example of how to reproduce the error:
function foo(bar, baz) {
baz()
}
foo(() => {})

Render HTML-File after Request an Database Query. (nodes.js, pug.js, pouch.db)

var url = require('url');
var pug = require('pug');
var PouchDB = require('pouchdb');
var db = new PouchDB('http://127.0.0.1:5984/data');
var doc = {
"_id": "mittens",
"name": "Mittens",
};
function query() {db.get('mittens', function (error, doc) {
if (error) {
console.log('Ops! There is an error.');
} else {
console.log(doc);
return doc;
}
});
}
module.exports = {
handleRequests: function(request, response) {
response.writeHead(200, {'Content-Type': 'text/html'});
var path = url.parse(request.url).pathname;
console.log(path);
switch (path) {
case '/':
response.write(pug.renderFile('./views/index.pug', query()));
response.end();
break;
The query() function is returning an Object with "name". But it isn't rendered by pug.js.
Why pug.js do not render the Template with doc.name?
As stated in comments by #Paul you couldn't simply return a value from an asynchronous function call. you should use callbacks or promises:
The callback way:
function query(item, callback) {
db.get(item, function (error, doc) {
if (error) {
callback(error, null);
} else {
callback(null, doc);
}
});
}
Then:
case '/':
query('mittens', function(err, doc) {
if (err) throw err;
response.write(pug.renderFile('./views/index.pug', doc));
response.end();
}
break;
Read More: How do I return the response from an asynchronous call? And implement promise way if you prefer.

How to get a function value on MongoDB collection.find()

When I run collection.find() in MongoDB/Node/Express, I need to return value for my array like this but iam in callback hell;
foursquare.getVenues(params,function(error, venues) {
if (!error) {
var places = [];
venues.response.venues.forEach(function(e) {
places.push(
{
obj_id:e.id,
name:e.name,
distance:e.distance,
here_now:req.collection.findById(e.id) //count- i want need this value
}
);
});
res.send(places);
}
});
You can try to use Async https://github.com/caolan/async#each
var async = require('async');
...
foursquare.getVenues(params, function (error, venues) {
if (!error) {
throw err;
}
var places = [];
async.each(venues.response.venues, function (e, callback) {
db.collection.findById(e.id, function (err, res) {
places.push({
obj_id: e.id,
name: e.name,
distance: e.distance,
here_now: res
});
callback()
});
}, function (err) {
if (err) {
console.log('A file failed to process');
} else {
console.log('All files have been processed successfully');
res.send(places);
}
});
});
or Using async.map
var async = require('async');
var createArray = function (e, cb) {
db.collection.findById(e.id,function(err,res){
var obj = {
obj_id: e.id,
name: e.name,
distance: e.distance,
here_now: res
}
cb(null, obj);
});
}
async.map(venues.response.venues, createArray, function (err, places) {
if(err){
throw err;
}
console.log(places);
});

Error Handling middleware in ExpressJS for spawn of multiple child_process

I have written a nice little error reporting middleware, that sits after all the GET and POST handling (after app.use(app.router); ). See below.
This works great for simple quick GET and POST that goes to the PostGIS database etc.
But I have one POST request that is designed to create a bunch of directories, a number of files, and then spawn 1 -> 8 child_processes tasks
childProcess.execFile(job.config.FMEPath, ["PARAMETER_FILE", job.fmeParamFile], { cwd: job.root },
All that setup does not take much time (less than a second, and it is all async (I use the async library at one point to sequence 5 steps (see below).
My issue is error handling. Right now I return a response immediately before creating all the files and doing all the steps. This means that next(err) is not working as expected. What is a good paradigm for reporting back the errors? I am using WINSTON to log errors [logger.log() ], but should I just log the errors on the server, or should I also report it to the original request. here is the current post request (and remember, I would have to keep the rest, and req and next object around for quite a while to be able to call next(err).
exports.build = function (req, res, next) {
var config = global.app.settings.config;
var jobBatch = groupJobs(req.body.FrameList);
var ticket = tools.newGuid("", true);
var fileCount = req.body.FrameList.length * nitfMultiplier;
var ts = timespan.fromSeconds(fileCount / config.TileRate);
var estimate = ts.hours + ":" + tools.pad(ts.minutes, 2) + ":" + tools.pad(ts.seconds, 2);
res.set({ 'Content-Type': 'application/json; charset=utf-8' });
res.send({ ticket: ticket, maxTiles: fileCount, timeEstimate: estimate, tileRate: config.TileRate, wwwURL: config.WWWUrl });
jobBatchRoot(req, res, jobBatch, config, ticket, next);
};
jobBatchRoot() (I will then go off and do a lot of processing, I did not include all that code.
exports.bugs = function (err, req, res, next) {
global.app.settings.stats.errors += 1;
if (err.code == undefined) {
err.code = 500;
err.message = "Server Error";
}
res.status(err.code);
logger.log('error', '%s url: %s status: %d \n', req.method, req.url, err.code, { query: req.query, body: req.body, message: err.message, stack: err.stack });
var desc = req.method + " " + req.url;
var body = util.format("%j", req.body);
var query = util.format("%j", req.query);
var stack = err.stack.split('\n');
res.format({
text: function () {
res.send(util.format("%j", { title: err.message, code: err.code, desc: desc, query: query, message: err.message, stack: err.stack, body: body}));
},
html: function () {
query = tools.pretty(req.query);
res.render('error', { title: err.message, code: err.code, desc: desc, query: query, message: err.message, stack: stack, body: body });
},
json: function () {
res.send({ title: err.message, code: err.code, desc: desc, query: query, message: err.message, stack: err.stack, body: body });
}
});
};
Perhaps I should be re-factoring this (maybe object oriented), anyway I thought I would post the full module here and all I am looking for is a few tips on structure, best practices.
var util = require('util');
var query = require("pg-query");
var timespan = require('timespan');
var _ = require('lodash');
var path = require('path');
var fs = require('fs');
var query = require("pg-query");
var async = require("async");
var childProcess = require("child_process");
var tools = require("../tools/tools");
var nitfMultiplier = 99;
var manifestVersionID = 5;
exports.setup = function (app) {
};
exports.estimate = function (req, res, next) {
var config = global.app.settings.config;
var fileCount = req.body.FrameList.length * nitfMultiplier;
var ts = timespan.fromSeconds(fileCount / config.TileRate);
var estimate = ts.hours + ":" + tools.pad(ts.minutes, 2) + ":" + tools.pad(ts.seconds, 2);
res.set({ 'Content-Type': 'application/json; charset=utf-8' });
res.send({ ticket: "Estimate", maxTiles: fileCount, timeEstimate: estimate, tileRate: config.TileRate, wwwURL: config.WWWUrl });
};
exports.build = function (req, res, next) {
var config = global.app.settings.config;
var jobBatch = groupJobs(req.body.FrameList);
var ticket = tools.newGuid("", true);
var fileCount = req.body.FrameList.length * nitfMultiplier;
var ts = timespan.fromSeconds(fileCount / config.TileRate);
var estimate = ts.hours + ":" + tools.pad(ts.minutes, 2) + ":" + tools.pad(ts.seconds, 2);
res.set({ 'Content-Type': 'application/json; charset=utf-8' });
res.send({ ticket: ticket, maxTiles: fileCount, timeEstimate: estimate, tileRate: config.TileRate, wwwURL: config.WWWUrl });
jobBatchRoot(req, res, jobBatch, config, ticket, next);
};
function groupJobs(list) {
var jobBatch = {};
_.forEach(list, function (obj) {
if (jobBatch[obj.type] == undefined) {
jobBatch[obj.type] = [];
}
jobBatch[obj.type].push(obj);
});
return jobBatch;
};
function jobBatchRoot(req, res, jobBatch, config, ticket, next) {
var batchRoot = path.join(config.JobsPath, ticket);
fs.mkdir(batchRoot, function (err) {
if (err) return next(err);
var mapInfoFile = path.join(batchRoot, "MapInfo.json");
var mapInfo = {
Date: (new Date()).toISOString(),
Version: manifestVersionID,
Zoom: req.body.Zoom,
CenterLat: req.body.CenterLat,
CenterLon: req.body.CenterLon
};
fs.writeFile(mapInfoFile, tools.pretty(mapInfo), function (err) {
if (err) return next(err);
spawnJobs(req, res, batchRoot, mapInfo, config, ticket, jobBatch, next);
});
});
};
function spawnJobs(req, res, root, mapInfo, config, ticket, jobBatch, next) {
_.forEach(jobBatch, function (files, key) {
var job = {
req: req,
res: res,
type: key,
files: files,
batchRoot: root,
mapInfo: mapInfo,
config: config,
ticket: ticket,
missingFiles: [],
run: true,
next: next
};
setup(job);
});
};
function setup(job) {
job.root = path.join(job.batchRoot, job.type);
job.fmeParamFile = path.join(job.root, "fmeParameters.txt");
job.fmeWorkSpace = path.join(job.config.LibrarianPath, "TileBuilder.fmw");
job.fmeLogFile = path.join(job.root, "jobLog.log");
job.errorLog = path.join(job.root, "errorLog.log");
job.jobFile = path.join(job.root, "jobFile.json");
job.manifestFile = path.join(job.root, "manifest.json");
async.series({
one: function (callback) {
maxZoom(job, callback);
},
two: function (callback) {
fs.mkdir(job.root, function (err) {
if (err) return job.next(err);
callback(null, "Job Root Created");
});
},
three: function (callback) {
makeParamFile(job, callback);
},
four: function (callback) {
delete job.req;
delete job.res;
fs.writeFile(job.jobFile, tools.pretty(job), function (err) {
if (err) return job.next(err);
callback(null, "Wrote Job File");
});
},
five: function (callback) {
runJob(job, callback);
},
six: function (callback) {
tileList(job, callback);
},
seven: function (callback) {
finish(job, callback);
},
},
function (err, results) {
if (err) return job.next(err);
console.log(tools.pretty(results));
});
}
function maxZoom(job, callback) {
var qString = util.format('SELECT type, "maxZoom" FROM portal.m_type WHERE type=\'%s\'', job.type);
query(qString, function (err, rows, result) {
if (err) {
var err = new Error(queryName);
err.message = err.message + " - " + qString;
err.code = 400;
return job.next(err);
}
job.maxZoom = rows[0].maxZoom - 1; // kludge for 2x1 root layer in leaflet
return callback(null, "Got MaxZoom");
});
}
function makeParamFile(job, callback) {
var text = util.format("%s\n", job.fmeWorkSpace);
text += util.format("--OutputDir %s\n", job.root);
text += util.format("--LogFile %s\n", job.fmeLogFile);
var source = "";
_.forEach(job.files, function (file) {
var path = ('development' == process.env.NODE_ENV) ? file.path.replace(job.config.SourceRootRaw, job.config.SourceRoot) : file.path;
if (fs.existsSync(path)) {
source += wrap(path, '\\"') + " ";
}
else {
job.missingFiles.push(path);
}
});
source = wrap(wrap(source, '\\"'), '"');
text += "--Sources " + source;
if (job.missingFiles.length == job.files.length) job.run = false;
fs.writeFile(job.fmeParamFile, text, function (err) {
if (err) return job.next(err);
return callback(null, "Wrote Paramaters File");
})
};
function wrap(content, edge) {
return edge+content+edge;
}
function runJob(job, callback) {
if (!job.run) return callback(null, "Skipped JOB, no files");
childProcess.execFile(job.config.FMEPath, ["PARAMETER_FILE", job.fmeParamFile], { cwd: job.root },
function (err, stdout, stderr) {
if (err) return job.next(err);
job.stdout = stdout;
job.stderr = stderr;
var bar = "\n--------------------------------------------------------------------------------------------------------\n";
var results = util.format("%s STDOUT: \n %s%s STDERR: \n %s", bar, job.stdout, bar, job.stderr);
fs.appendFile(job.fmeLogFile, results, function (err) {
return callback(err, "FME JOB " + job.type + " run completed");
});
});
}
function tileList(job, callback) {
var tiles = [];
var byteCount = 0;
fs.readdir(job.root, function (err, files) {
if (err) {
logger.log('error', 'tileList directory read: %s \n', job.root, { message: err.message, stack: err.stack });
return job.next(err);
}
async.each(files, function (file, done) {
var fileName = file.split(".");
fs.lstat(job.root + "\\" + file, function (err, stats) {
if (!err && stats.isFile() && (fileName[1] == "png")) {
tiles.push({ id: fileName[0], size: stats.size });
byteCount += stats.size;
};
done(null);
});
}, function (err) {
job.tileList = tiles;
job.byteCount = byteCount;
return callback(null, "got tile list");}
);
});
}
function finish(job, callback) {
var manifest = {
Date: (new Date()).toISOString(),
Version: manifestVersionID,
MaxZoom: job.maxZoom,
Class: "OVERLAY",
FileCount: job.tileList.length,
Size: job.byteCount / (1024 * 1024), // Mbytes
files: job.tileList
};
fs.writeFile(job.manifestFile, tools.pretty(manifest), function (err) {
if (err) {
logger.log('error', 'manifest write: %s \n', job.manifestFile, { message: err.message, stack: err.stack });
return job.next(err);
}
return callback(null, "JOB " + job.type + " completed");
});
}
I went and re-factored this. I created a module, with module.exports = function(..) {...}
and then added lots of state and methods to create a class. That contains the Job definition. So now I create the top level directories, return a response, and spawn the sub jobs. They all run async after the express response. But they should not get errors, and if they do, then I use WINSTON to log them in the server, and also return a job done information to the user when all the builds are done.

Resources