FOR LOOP in Node js needing the result of the previous iteration - node.js

I need to do a loop in Node JS where I need the result of the previous iteration.
function jsonParser(txt_file, cb) {
var perc = 0;
lines = txt_file.split('\n');
insert_communication = 'INSERT INTO communication (account_id, contact_id, type_com_id, coin_id, com_datetime, ' +
'destination_key, destination_st, duration, cost, description, discount) VALUES ';
insert_internet = 'INSERT INTO internet_data (account_id, contact_id, type_com_id, coin_id, com_datetime, ' +
'duration, cost, description, discount) VALUES ';
for (let i = 2; i < lines.length; i++) {
readTextFile2(lines[i], function (cb) {
if (cb[0] == "communication")
insert_communication += cb[1] + ',';
if (cb[0] == "internet")
insert_internet += cb[1] + ',';
});
}
cb(insert_communication);
}
How I am supposed to achieve this?
Thanks

for (let i = 2; i < lines.length; i++) {
const previous = lines[i - 1 ];
const hasPrevious = !!previous;
if(hasPrevious) {
//ok you have a previously value, now you could use it
}
readTextFile2(lines[i], function (cb) {
if (cb[0] == "communication")
insert_communication += cb[1] + ',';
if (cb[0] == "internet")
insert_internet += cb[1] + ',';
});
}

Related

Transform item receipt looping forever

I don't know why but the logs are looping forever and then I get an error of time executed
This is the code.
function checkForQuantity(irList, irRec, poId) {
try {
var sublistId = 'item'
var count = irRec.getLineCount('item')
log.debug('irList', irList);
log.debug('count', count); Here count = 5
for (var i = 0; i < count; i++) {
log.debug('i', i) Here i get only logs of i = 1 and i = 2
var irData = irList[i];
log.debug('checkForQuantity | irData ' + i, irData);
irRec.setSublistValue(sublistId, 'quantity', i, '')
var itemId = getItemId(irData.item)
var quantity = irData.quantity
for (var i = 0; i < count && quantity; i++) {
var item = irRec.getSublistValue(sublistId, 'item', i)
if (item == itemId) {
var qtyRem = irRec.getSublistValue(sublistId,
'quantityremaining', i)
var diff = qtyRem - quantity
if (diff >= 0) {
irRec.setSublistValue(sublistId, 'quantity', i, quantity)
break;
} else {
quantity -= qtyRem
irRec.setSublistValue(sublistId, 'quantity', i, qtyRem)
}
}
}
}
} catch (e) {
log.error('checkForQuantity (for poId: ' + poId + ')', e)
}
on the debug.log('i', i)
i am getting only i = 1, i =2
count is 5 but still i cant loop on 3 and 4 . why is that?
In this edition of what Brian said, we change the second loop control variable to j.
function checkForQuantity(irList, irRec, poId) {
try {
var sublistId = 'item'
var count = irRec.getLineCount('item')
log.debug('irList', irList);
log.debug('count', count); Here count = 5
for (var i = 0; i < count; i++) {
log.debug('i', i) Here i get only logs of i = 1 and i = 2
var irData = irList[i];
log.debug('checkForQuantity | irData ' + i, irData);
irRec.setSublistValue(sublistId, 'quantity', i, '')
var itemId = getItemId(irData.item)
var quantity = irData.quantity
for (var j = 0; j < count && quantity; j++) {
var item = irRec.getSublistValue(sublistId, 'item', j)
if (item == itemId) {
var qtyRem = irRec.getSublistValue(sublistId,
'quantityremaining', j)
var diff = qtyRem - quantity
if (diff >= 0) {
irRec.setSublistValue(sublistId, 'quantity', j, quantity)
break;
} else {
quantity -= qtyRem
irRec.setSublistValue(sublistId, 'quantity', j, qtyRem)
}
}
}
}
} catch (e) {
log.error('checkForQuantity (for poId: ' + poId + ')', e)
}

Column length is returning zero when I pass the value i in the xpath using for loop in nodejs webdriver

This is the code to iterate through a dynamic table.
driver.findElements(By.xpath(`//*[#id="slide-wrapper"]/div/ui-view/search-activities/div/div[2]/table/tbody/tr`)).then(function(rows) {
console.log("NoofRowsinthetable" + rows.length);
var identifyvalue = "6/16/17 12:41 PM"
var datacount = 0;
for (var i = 1; i <= rows.length; i++) {
driver.findElements(By.xpath(`//*[#id="slide-wrapper"]/div/ui-view/search-activities/div/div[2]/table/tbody/tr[i]/td`)).then(function (cells) {
console.log("NoofColumnsinthetable" + cells.length);
for (var j = 1; j <= cells.length; j++) {
driver.findElement(By.xpath(`//*[#id="slide-wrapper"]/div/ui-view/search-activities/div/div[2]/table/tbody/tr[i]/td[j]`)).getText().then(function (cell_text) {
console.log(cell_text);
if (identifyvalue === cell_text) {
datacount = datacount + 1;
console.log("Data count" + datacount);
}
})
First Forloop:
Cells.length returns zero even if there are 9 columns.
IF I AM PASSING THE VALUE OF I AS STRING(tr["+i+"]) THEN IT IS RETURNING THE VALUE OF COLUMN.LENGTH OF FIRST ROW ONLY AND IF I DO NOT PASS THE VALUE OF ā€˜Iā€™ AS STRING THEN IT IS RETURING THE COLUMN.LENGTH AS ZERO.
Second For loop:
IF I AM PASSING THE VALUE OF I AND J AS STRING(tr["+i+"]/td["+j+"]), THEN IT IS RETURNING THE VALUE OF FIRST COLUMN ONLY
The problem is with single quote. you are starting the string with single quote and it should be ended with single. try the following,
driver.findElements(By.xpath(`//*[#id="slide-wrapper"]/div/ui-view/search-activities/div/div[2]/table/tbody/tr`)).then(function(rows) {
console.log("NoofRowsinthetable" + rows.length);
var identifyvalue = "6/16/17 12:41 PM"
var datacount = 0;
for (var i = 1; i <= rows.length; i++) {
driver.findElements(By.xpath(`//*[#id="slide-wrapper"]/div/ui-view/search-activities/div/div[2]/table/tbody/tr[`+i+`]/td`)).then(function (cells) {
console.log("NoofColumnsinthetable" + cells.length);
for (var j = 1; j <= cells.length; j++) {
driver.findElement(By.xpath(`//*[#id="slide-wrapper"]/div/ui-view/search-activities/div/div[2]/table/tbody/tr[`+i+`]/td[`+j+`]`)).getText().then(function (cell_text) {
console.log(cell_text);
if (identifyvalue === cell_text) {
datacount = datacount + 1;
console.log("Data count" + datacount);
}
})

Web Service in Nodejs

I want to print out all number from 1 to 1,000,000 with 1,000 numbers per line. and return the response without having a long delay.
Thanks in advance for any type of help!!!
Well, first you can create a function to print numbers from 1 to 1000.
function getThousand(index) {
var result = '';
for (var i = index; i < index + 1000; i++) {
result += i + ' ';
}
return result;
}
Then, you need a function to call this for 1 to 1000000.
function getAll() {
var result = '';
for (var i = 0; i < 1000; i++) {
result = getThousand((i * 1000) + 1) + " \n";
fs.appendFileSync('foo.txt', result);
}
}
Then call it all:
getAll();
This will save your lines into a file. At the end of getAll() you can print what you need.

SP.NavigationNode.get_isVisible() broken?

I need to read the "Top Nav", the "Children Nodes" and check if each node is visible.
I am using JSOM to accomplish this. Everything is working fine except for the get_isVisible() function. It always returns true. MSDN: http://msdn.microsoft.com/en-us/library/office/jj246297.aspx
I am on a publishing site in 2013 and I know some of the items are hidden. (My web and context are defined outside of this snippet)
var visParents = [], visChildren = [];
var topNodes = web.get_navigation().get_topNavigationBar();
context.load(topNodes);
context.executeQueryAsync(onQuerySucceeded, onQueryFailed)
function onQuerySucceeded() {
var nodeInfo = '';
var nodeEnumerator = topNodes.getEnumerator();
while (nodeEnumerator.moveNext()) {
var node = nodeEnumerator.get_current();
nodeInfo += node.get_title() + '\n';
if (node.get_isVisible())
visParents.push(node);
}
console.log("Current nodes: \n\n" + nodeInfo);
console.log("Visible Parents", visParents)
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
It is a known issue, it seems that SP.NavigationNode.isVisible property does not correspond to the property that indicates whether navigation node is hidden or shown.
Please refer "Hidden" property of SPNavigationNode for a details
The following function demonstrates how to retrieve hidden node Urls:
function getGlobalNavigationExcludedUrls(Success,Error)
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var subwebs = web.get_webs();
var pagesList = web.get_lists().getByTitle("Pages");
var pageItems = pagesList.getItems(SP.CamlQuery.createAllItemsQuery());
var allProperties = web.get_allProperties();
context.load(web);
context.load(subwebs);
context.load(allProperties);
context.load(pageItems);
context.executeQueryAsync(
function() {
var excludedIds = allProperties.get_item('__GlobalNavigationExcludes').split(';');
var exludedUrls = [];
for (var i = 0; i < excludedIds.length - 1; i++ )
{
for (var j = 0; j < subwebs.get_count(); j++ )
{
var subweb = subwebs.getItemAtIndex(j);
if(subweb.get_id().toString() == excludedIds[i]){
exludedUrls.push(subweb.get_serverRelativeUrl());
break;
}
}
for (var j = 0; j < pageItems.get_count(); j++ )
{
var pageItem = pageItems.getItemAtIndex(j);
if(pageItem.get_item('UniqueId').toString() == excludedIds[i]){
exludedUrls.push(web.get_serverRelativeUrl() + pageItem.get_item('FileRef'));
break;
}
}
}
Success(exludedUrls);
},
Error
);
}
//Usage: print excluded nodes Urls
getGlobalNavigationExcludedUrls(function(excludedNodeUrls){
for (var j = 0; j < excludedNodeUrls.length; j++ )
{
console.log(excludedNodeUrls[j]);
}
},
function(sender,args){
console.log(args.get_message());
});

Text version compare in FCKEditor

Am using Fck editor to write content. Am storing the text as versions in db. I want to highlight those changes in versions when loading the text in FCK Editor.
How to compare the text....
How to show any text that has been deleted in strike through mode.
Please help me/...
Try google's diff-patch algorithm http://code.google.com/p/google-diff-match-patch/
Take both previous and current version of the text and store it into two parameters. Pass the two parameters to the following function.
function diffString(o, n) {
o = o.replace(/<[^<|>]+?>| /gi, '');
n = n.replace(/<[^<|>]+?>| /gi, '');
var out = diff(o == "" ? [] : o.split(/\s+/), n == "" ? [] : n.split(/\s+/));
var str = "";
var oSpace = o.match(/\s+/g);
if (oSpace == null) {
oSpace = ["\n"];
} else {
oSpace.push("\n");
}
var nSpace = n.match(/\s+/g);
if (nSpace == null) {
nSpace = ["\n"];
} else {
nSpace.push("\n");
}
if (out.n.length == 0) {
for (var i = 0; i < out.o.length; i++) {
str += '<span style="background-color:#F00;"><del>' + escape(out.o[i]) + oSpace[i] + "</del></span>";
}
} else {
if (out.n[0].text == null) {
for (n = 0; n < out.o.length && out.o[n].text == null; n++) {
str += '<span style="background-color:#F00;"><del>' + escape(out.o[n]) + oSpace[n] + "</del></span>";
}
}
for (var i = 0; i < out.n.length; i++) {
if (out.n[i].text == null) {
str += '<span style="background-color:#0C0;"><ins>' + escape(out.n[i]) + nSpace[i] + "</ins></span>";
} else {
var pre = "";
for (n = out.n[i].row + 1; n < out.o.length && out.o[n].text == null; n++) {
pre += '<span style="background-color:#F00;"><del>' + escape(out.o[n]) + oSpace[n] + "</del></span>";
}
str += " " + out.n[i].text + nSpace[i] + pre;
}
}
}
return str;
}
this returns an html in which new text is marked green and deleted text as red + striked out.

Resources