How can I get node.js to return data once all operations are complete
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further 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 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);
});
|
xsl and xpath : search for a node inside a node, and return the value of an attribute in the found node
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I have the below xml : , A basic XPath expression that will extract what you need is //w:style[w:name/@w:val = 'Peter']/@w:styleId
<xsl:key name="nameByVal" match="w:name" use="@w:val" />
key('nameByVal', 'Peter')/../@w:styleId
key('nameByVal', 'Peter')/ancestor::w:style/@w:styleId
|
how to get complete parent node name in js tree while selecting the child node
Date : March 29 2020, 07:55 AM
it helps some times I am getting the child tree name but I want to get its complete hierarchy of parent node names. , This will work fine... it will get the full parents... $('#bdeViewNew').on('select_node.jstree', function (e, data) {
var loMainSelected = data;
uiGetParents(loMainSelected);
});
function uiGetParents(loSelectedNode) {
try {
var lnLevel = loSelectedNode.node.parents.length;
var lsSelectedID = loSelectedNode.node.id;
var loParent = $("#" + lsSelectedID);
var lsParents = loSelectedNode.node.text + ' >';
for (var ln = 0; ln <= lnLevel -1 ; ln++) {
var loParent = loParent.parent().parent();
if (loParent.children()[1] != undefined) {
lsParents += loParent.children()[1].text + " > ";
}
}
if (lsParents.length > 0) {
lsParents = lsParents.substring(0, lsParents.length - 1);
}
alert(lsParents);
}
catch (err) {
alert('Error in uiGetParents');
}
}
|
Can’t gracefully shut down a node + Express app (node http server's close function does not complete)
Date : November 12 2020, 11:01 PM
To fix this issue .close() doesn't actually close any active connections, it will only stop new connections from being made, and it'll wait for the active connections to get closed (by the client). If clients are connected that keep a connection open (say using a keep-alive connection), the callback may never get called.
|
How to analyze complete response of Node-RED HTTP Node
Date : March 29 2020, 07:55 AM
it helps some times The output to the debug tab is deliberately trimmed to stop it overwhelming the websocket connection to the browser with too much data. If you change the debug node to output to the "debug tab and console" the full message will be output to Node-RED logs and you should be able to compare them.
|