I'm new to nodejs.
Here is my .js file. I'm trying to expose audioData variable to other functions. audioData variable value is being empty outside the function. I see the value when I print inside the function. What could be wrong?
'use strict';
var asyncrequest = require('request');
var xml2js = require('xml2js');
var parseString = xml2js.parseString;
var audioData = [];
asyncrequest("http://example.com/feed", function(error, responsemeta, body) {
parseString(body, function(err, result){
var stories = result['rss']['channel'][0]['item'];
console.log("Total stories: " + stories.length);
stories.forEach(function(entry) {
var singleObj = {}
singleObj['title'] = entry['title'][0];
singleObj['value'] = entry['enclosure'][0].$.url;
audioData.push(singleObj);
});
});
console.dir(audioData);
});
module.exports = audioData;
console.log("Program ended");
You'll have to return a promise for the audioData, not the audioData itself! You can learn more about promises elsewhere. Happily there's a promisified version of request, request-promise, that you can use like so:
'use strict';
var rp = require('request-promise');
var xml2js = require('xml2js');
var parseString = xml2js.parseString;
var audioData = [];
var promiseForAudioData = rp('http://example.com/feed')
.then(body => {
parseString(body, function(err, result){
var stories = result['rss']['channel'][0]['item'];
console.log("Total stories: " + stories.length);
stories.forEach(function(entry) {
var singleObj = {}
singleObj['title'] = entry['title'][0];
singleObj['value'] = entry['enclosure'][0].$.url;
audioData.push(singleObj);
});
});
return audioData;
})
.catch(console.error.bind(console));
module.exports = promiseForAudioData;
console.log("Program ended");
If you don't want to use promises, you can either export inside the callback or export the request method itself.
asyncrequest("http://example.com/feed", function(error, responsemeta, body) {
parseString(body, function(err, result){
var stories = result['rss']['channel'][0]['item'];
console.log("Total stories: " + stories.length);
stories.forEach(function(entry) {
var singleObj = {}
singleObj['title'] = entry['title'][0];
singleObj['value'] = entry['enclosure'][0].$.url; audioData.push(singleObj);
});
module.exports = audioData;
});
});
// Or
exports.get = function (callback) {
return asyncrequest(/* ... */, callback);
}
// Other module
require("./module").get(function (audioData) {
/* Do something */
})
Related
I have writen simple JS object which has function csvFileToArray. Function should return parsed CSV array.
The problem is that I don't have output from anonymous function which is passed to readFileSync.
test1 is loged to console correctly but test2 is not.
It's my first time with node.js What am I doing wrong?
var passwdArray = [];
var csv = function () {
this.csvFileToArray = function (fileName, delimiter) {
console.log("test1");
var fs = require('fs');
fs.readFileSync(fileName, 'utf8', function(err, data) {
console.log("test2");
if (err) {
throw err;
} else {
var csvLineArray = data.split("\n");
var csvArray = [];
csvArray['header'] = csvLineArray[0].split(delimiter);
csvArray['data'] = [];
for(var i = 1; i < csvLineArray.length; i++) {
csvArray['data'].push(csvLineArray[i].split(delimiter));
}
return csvArray;
}
});
}
}
var csvHandler = new csv();
var test =csvHandler.csvFileToArray('test.csv', ',');
console.log(test);
You are using readFileSync function and it's working sync. So you can not use callback inside it. DOC
So you can use it like:
var passwdArray = [];
var csv = function () {
this.csvFileToArray = function (fileName, delimiter) {
console.log("test1");
var fs = require('fs');
var data = fs.readFileSync(fileName, 'utf8');
var returnedData = doSomething(null,data);
console.log(returnedData);
}
};
function doSomething(err, data) {
console.log("test2");
if (err) {
throw err;
} else {
var csvLineArray = data.split("\n");
var csvArray = [];
csvArray['header'] = csvLineArray[0].split(delimiter);
csvArray['data'] = [];
for(var i = 1; i < csvLineArray.length; i++) {
csvArray['data'].push(csvLineArray[i].split(delimiter));
}
return csvArray;
}
};
var csvHandler = new csv();
var test =csvHandler.csvFileToArray('test.csv', ',');
console.log(test);
If you want to use it async you can use readFile function.
Are you mixing up fs.readFileSync(path[, options]) and fs.readFile(path[, options], callback)File System | Node.js v8.1.4 Documentation ?
The method you are using does not accept a callback parameter.
I have a Node.Js web app with Express.Js that reads values from xml files, store values from all xml files into an array with sub-array represent the separation of per xml file. At the moment, I have the following code on the Node:
app.get('/get_software_requests', function (req, res) {
console.log("loading software requests");
requests_callback(function(all_software_requests){
console.log(all_software_requests);
});
function requests_callback(callback){
loadAllSoftwareRequests(function(all_software_requests){
callback(all_software_requests);
});
}
});
function loadAllSoftwareRequests(callback){
console.log("loading requests");
fs.readdir("/project_requests", function(error, files) {
files.forEach(filename => {
var software_request = new Array();
loadSoftwareRequestXML(filename, software_request, function(software_request){
all_software_requests.push(software_request);
callback(all_software_requests);
});
});
});
}
function loadSoftwareRequestXML(filename, software_request, callback){
var xmlparser = new xml2js.Parser();
var filepath = "/project_requests/" + filename;
fs.readFile(filepath, "utf-8", function(error, values){
xmlparser.parseString(values, function(error, xmlfile){
var xmldata = xmlfile;
date_requested = xmldata.ProjectRequest.DateRequested;
client_org = xmldata.ProjectRequest.ClientOrganization;
proposed_budget = xmldata.ProjectRequest.ProposedBudget;
contact_name = xmldata.ProjectRequest.ContactName;
delivery_date = xmldata.ProjectRequest.DeliveryDate;
requirements = xmldata.ProjectRequest.UserRequirements;
software_request.push(date_requested);
software_request.push(client_org);
callback(software_request);
});
});
}
So far, for "console.log(all_software_requests);" on the main app.get, the console outputs:
I want the Node to only return the last iteration result, like
Any help or suggestion is appreciated. Please feel free to comment. Thanks.
You can use native promises for that:
Promise.all(files.map(
filename => new Promise(ok => load(filename, request => ok(request)))
)).then(requests => callback(requests));
I mess with promise/callback style here to minify your code editions.
Better to use promises instead of callbacks in client code too.
Then it becomes just:
let loadAll = files => Promise.all(files.map(load));
let load = filename => {/*return some promise with result*/}
Without promises at all it is not too difficult:
let c = files.length; // initialize a counter
files.forEach(filename => {
var software_request = new Array();
loadSoftwareRequestXML(filename, software_request, function(software_request){
all_software_requests.push(software_request);
if(!--c) { // all async calls are finished
callback(all_software_requests);
}
});
});
You could also add my "next" method that will handle the looping for you. This has worked well for me in these types of situations.
app.get('/get_software_requests', function (req, res) {
console.log("loading software requests");
requests_callback(function(all_software_requests){
console.log(all_software_requests);
});
function requests_callback(callback){
loadAllSoftwareRequests(function(all_software_requests){
callback(all_software_requests);
});
}
});
function loadAllSoftwareRequests(callback){
console.log("loading requests");
fs.readdir("/project_requests", function(error, files) {
files.forEach(filename => {
var software_request = new Array();
loadSoftwareRequestXML(filename, software_request, function(software_request){
all_software_requests.push(software_request);
callback(all_software_requests);
});
});
});
}
function loadSoftwareRequestXML(filename, software_request, callback){
var xmlparser = new xml2js.Parser();
var filepath = "/project_requests/" + filename;
fs.readFile(filepath, "utf-8", function(error, values){
var index = 0;
var next = function () {
if (index >= values.length) {
callback(null, values);
return;
}
var value = values[index];
xmlparser.parseString(value, function(error, xmlfile){
var xmldata = xmlfile;
date_requested = xmldata.ProjectRequest.DateRequested;
client_org = xmldata.ProjectRequest.ClientOrganization;
proposed_budget = xmldata.ProjectRequest.ProposedBudget;
contact_name = xmldata.ProjectRequest.ContactName;
delivery_date = xmldata.ProjectRequest.DeliveryDate;
requirements = xmldata.ProjectRequest.UserRequirements;
software_request.push(date_requested);
software_request.push(client_org);
value.software_request = software_request;
index++;
next()
});
}
next();
});
}
Trying to create a lambda function that lists tagged ec2 and tagged rds and performs an action thereafter. This is the first time i have used javascript and would appreciate some help.
Please see my example below
var aws = require('aws-sdk');
var ec2 = new aws.EC2();
var rds = new aws.RDS();
aws.config.region = 'us-east-1';
exports.handler = function(event,context) {
if (event) {
console.log(event.id);
}
//setup params for rds call
var rdsparams = {
DBInstanceIdentifier: 'master',
};
//setup params for ec2 call
var ec2params = {
Filters: [
{
Name: 'tag:role',
Values: [
'app'
],
}
]
};
//Get ec2 instances with app tag, may need to add a condition on running so pulled it into hash
ec2.describeInstances(ec2params, function(err, appdata) {
if (err) {
console.log(err);
return;
}
else {
var apparray={};
for(var i = 0; i < appdata.Reservations.length; i++) {
var ins = appdata.Reservations[i].Instances[0];
var id = ins.InstanceId;
var state = ins.State.Name;
apparray[id]=state;
}
console.log(apparray);
context.succeed(apparray);
}
});
rds.describeDBInstances(rdsparams, function(err, data) {
if (err) {
console.log(err, err.stack);
return;
}
else {
var rdsarray={};
var rdsarray=(data);
console.log(rdsarray);
var ins=rdsarray[0];
var name = ins.ReadReplicaDBInstanceIdentifiers[0];
rdsarray[replicant]=name;
}
context.succeed(rdsarray);
});
//context.done();
};
I want to return my filtered (apparray) and (rdsarray) back from my functions and perform a calculation on this within the main body of the script. Any ideas on how to do this.
something like
var replicas = rdsarray.length for example
Thanks in advance
var aws = require('aws-sdk');
var ec2 = new aws.EC2();
var rds = new aws.RDS();
aws.config.region = 'us-east-1';
exports.handler = function(event, context) {
if (event) {
console.log(event.id);
}
//setup params for rds call
var rdsparams = {
DBInstanceIdentifier: 'master',
};
//setup params for ec2 call
var ec2params = {
Filters: [{
Name: 'tag:role',
Values: [
'app'
],
}]
};
//Get ec2 instances with app tag, may need to add a condition on running so pulled it into hash
ec2.describeInstances(ec2params, function(err, appdata) {
if (err)
return context.done(err, null);
var apparray = {};
for (var i = 0; i < appdata.Reservations.length; i++) {
var ins = appdata.Reservations[i].Instances[0];
var id = ins.InstanceId;
var state = ins.State.Name;
apparray[id] = state;
}
console.log(apparray);
var resultData = {};
resultData.apparray = apparray;
rds.describeDBInstances(rdsparams, function(err, data) {
if (err)
return context.done(err, null);
var rdsarray = {};
var rdsarray = (data);
console.log(rdsarray);
var ins = rdsarray[0];
var name = ins.ReadReplicaDBInstanceIdentifiers[0];
rdsarray[replicant] = name;
resultData.rdsarray = rdsarray;
context.done(null, resultData);
});
});
};
and back in the code from you are calling the lambda function
var lambda = new sdk.Lambda();
var params = {
FunctionName: 'arn:aws:lambda:us-west-2:1541546477777:function:MyFunction',
Payload: JSON.stringify(/*your params here*/)
};
lambda.invoke(params, function(err, data) {
if (err) {
console.log('error ===', err);
return ;
}
var lambdaData = JSON.parse(data.Payload);
// do your stuff here
});
Is this what you needed? It may be broken but I hope you get the idea of it
I am trying to retrieve a json object to use it in another module, but I have a problem with callback. I have the error "callback is not a function". I use callback because my variable description is undefined, so i guess it's a problem of asynchronous.
Could you help me plz :)
var leboncoin = function () {
var http = require('http')
var bl = require('bl')
http.get("http://www.website.com", function (response, callback) {
response.pipe(bl(function (err, data) {
if (err) {
return console.error(err)
callback(err);
}
var data = data.toString()
var brand = ...
var model = ...
var releaseDate = ...
var km = ...
var fuel = ...
var gearbox = ...
description.Brand = brand;
description.Model = model;
description.Year = releaseDate;
description.KM = km;
description.Fuel = fuel;
description.Gearbox = gearbox;
callback(description);
return (description)
/*console.log(description.Brand);
console.log(description.Model);
console.log(description.Year);
console.log(description.KM);
console.log(description.Fuel);
console.log(description.Gearbox);*/
}))
})
}
exports.leboncoin = leboncoin;
var module = require('./leboncoin');
var res = module.leboncoin();
console.log(res);
Callbacks aren't magic that just appear. You need to define a parameter to your function and pass the callback you want to use.
// --------------------------v
var leboncoin = function (callback) {
var http = require('http')
var bl = require('bl')
http.get("http://www.website.com", function (response) {
response.pipe(bl(function (err, data) {
if (err) {
callback(err);
return;
}
var data = data.toString()
var description = { /* your description object */ }
callback(description);
}))
})
}
exports.leboncoin = leboncoin;
var module = require('./leboncoin');
// -----------------vvvvvvvv
module.leboncoin(function(res) {
console.log(res);
});
The method http.get requires a function that accepts only a parameter named response (or whatever you want, the name doesn't matter indeed), thus your second one, callback, is undefined and invoking it will end ever in that error.
I am just learning server-side JavaScript so please bear with any glaring mistakes I've made.
I am trying to write a file parser that operates on HTML files in a directory and returns a JSON string once all files have been parsed. I started it with a single file and it works fine. it loads the resource from Apache running on the same machine, injects jquery, does the parsing and returns my JSON.
var request = require('request'),
jsdom = require('jsdom'),
sys = require('sys'),
http = require('http');
http.createServer(function (req, res) {
request({uri:'http://localhost/tfrohe/Car3E.html'}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var window = jsdom.jsdom(body).createWindow();
jsdom.jQueryify(window, 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js', function (window, jquery) {
// jQuery is now loaded on the jsdom window created from 'body'
var emps = {};
jquery("tr td img").parent().parent().each(function(){
var step = 0;
jquery(this).children().each(function(index){
if (jquery(this).children('img').attr('src') !== undefined) {
step++;
var name = jquery(this).parent().next().next().children('td:nth-child('+step+')').children().children().text();
var name_parts = name.split(",");
var last = name_parts[0];
var name_parts = name_parts[1].split(/\u00a0/g);
var first = name_parts[2];
emps[last + ",_" + first] = jquery(this).children('img').attr('src');
}
});
});
emps = JSON.stringify(emps);
//console.log(emps);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(emps);
});
} else {
res.writeHead(200, {"Content-Type": "text/plain"});
res.end("empty");
//console.log(response.statusCode);
}
});
}).listen(8124);
Now I am trying to extend this to using the regular file system (fs) and get all HTML files in the directory and parse them the same way and return a single combined JSON object once all files have been parsed. Here is what I have so far but it does not work.
var sys = require("sys"),
fs = require("fs"),
jsdom = require("jsdom"),
emps = {};
//path = '/home/inet/www/media/employees/';
readDirectory = function(path) {
fs.readdir(path, function(err, files) {
var htmlfiles = [];
files.forEach(function(name) {
if(name.substr(-4) === "html") {
htmlfiles.push(name);
}
});
var count = htmlfiles.length;
htmlfiles.forEach(function(filename) {
fs.readFile(path + filename, "binary", function(err, data) {
if(err) throw err;
window = jsdom.jsdom(data).createWindow();
jsdom.jQueryify(window, 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js', function (window, jquery) {
jquery("tr td img").parent().parent().each(function(){
var step = 0;
jquery(this).children().each(function(index){
if (jquery(this).children('img').attr('src') !== undefined) {
step++;
var empname = jquery(this).parent().next().next().children('td:nth-child('+step+')').children().children().text();
var name_parts = empname.split(",");
var last = name_parts[0];
var name_parts = name_parts[1].split(/\u00a0/g);
var first = name_parts[2]
emps[last + ",_" + first] = jquery(this).children('img').attr('src');
}
});
});
});
});
});
});
}
readDirectory('/home/inet/www/media/employees/', function() {
console.log(emps);
});
In this particular case, there are 2 html files in the directory. If i console.log(emps) during the htmlfiles.forEach() it shows me the results from the first file then the results for both files together the way I expect. how do I get emps to be returned to readDirectory so i can output it as desired?
Completed Script
After the answers below, here is the completed script with a httpServer to serve up the detail.
var sys = require('sys'),
fs = require("fs"),
http = require('http'),
jsdom = require('jsdom'),
emps = {};
var timed = setInterval(function() {
emps = {};
readDirectory('/home/inet/www/media/employees/', function(emps) {
});
}, 3600000);
readDirectory = function(path, callback) {
fs.readdir(path, function(err, files) {
var htmlfiles = [];
files.forEach(function(name) {
if(name.substr(-4) === "html") {
htmlfiles.push(name);
}
});
var count = htmlfiles.length;
htmlfiles.forEach(function(filename) {
fs.readFile(path + filename, "binary", function(err, data) {
if(err) throw err;
window = jsdom.jsdom(data).createWindow();
jsdom.jQueryify(window, 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js', function (window, jquery) {
var imagecount = jquery("tr td img").length;
jquery("tr td img").parent().parent().each(function(){
var step = 0;
jquery(this).children().each(function(index){
if (jquery(this).children('img').attr('src') !== undefined) {
step += 1;
var empname = jquery(this).parent().next().next().children('td:nth-child('+step+')').children().children().text();
var name_parts = empname.split(",");
var last = name_parts[0];
var name_parts = name_parts[1].split(/\u00a0/g);
var first = name_parts[2]
emps[last + ",_" + first] = jquery(this).children('img').attr('src');
}
});
});
count -= 1;
if (count <= 0) {
callback(JSON.stringify(emps));
}
});
});
});
});
}
var init = readDirectory('/home/inet/www/media/employees/', function(emps) {
});
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify(emps));
}).listen(8124);
That sure is a lot of code a couple of mistakes.
You're never calling the callback function you supply to readDirectory
You need to keep track of the files you have parsed, when you parsed all of them, call the callback and supply the emps
This should work:
var sys = require("sys"),
fs = require("fs"),
jsdom = require("jsdom"),
//path = '/home/inet/www/media/employees/';
// This is a nicer way
function readDirectory(path, callback) {
fs.readdir(path, function(err, files) {
// make this local
var emps = {};
var htmlfiles = [];
files.forEach(function(name) {
if(name.substr(-4) === "html") {
htmlfiles.push(name);
}
});
// Keep track of the number of files we have parsed
var count = htmlfiles.length;
var done = 0;
htmlfiles.forEach(function(filename) {
fs.readFile(path + filename, "binary", function(err, data) {
if(err) throw err;
window = jsdom.jsdom(data).createWindow();
jsdom.jQueryify(window, 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js', function (window, jquery) {
jquery("tr td img").parent().parent().each(function(){
var step = 0;
jquery(this).children().each(function(index){
if (jquery(this).children('img').attr('src') !== undefined) {
step++;
var empname = jquery(this).parent().next().next().children('td:nth-child('+step+')').children().children().text();
var name_parts = empname.split(",");
var last = name_parts[0];
var name_parts = name_parts[1].split(/\u00a0/g);
var first = name_parts[2]
emps[last + ",_" + first] = jquery(this).children('img').attr('src');
}
});
});
// As soon as all have finished call the callback and supply emps
done++;
if (done === count) {
callback(emps);
}
});
});
});
});
}
readDirectory('/home/inet/www/media/employees/', function(emps) {
console.log(emps);
});
You seem to be doing this a tad wrong
readDirectory('/home/inet/www/media/employees/', function() {
console.log(emps);
});
But you've defined your function as:
readDirectory = function(path) {
Where is the callback argument? Try this:
readDirectory = function(path, callback) {
then under emps[last + ",_" + first] = jquery(this).children('img').attr('src'); put
callback.call(null, emps);
Your callback function will be called however many times your loop goes on for. If you want it to return all of them at once, you'll need to get a count of how many times the loop is going to run for, count up until that number then call your callback when the emps array is full of the data you need.