For loop going out of range? [duplicate] - javascript

This question already has answers here:
What is an off-by-one error and how do I fix it?
(6 answers)
Closed 4 months ago.
I am using a library in Javascript to pull in data from a chat server. The library is irrelevant but is quickblox. The data comes back into my app and can be seen, but when I try an loop on an object they return, it goes out of range, can't get 'last_message' of undefined. Loop should run for res.items[i].length which at the mo is two, but it is trying to carry on running, it seems.
var onDialogs = function(err, res){
console.log("------------------------------------List Of Dialogs------------------------------------",res);
var count = 0;
var sent;
var i = 0;
console.log("res.items.length",res.items.length)
for (i;i<=res.items.length;i++){
console.log("this one: ", res.items[i]);
if (res.items[i].last_message===null||res.items[i].last_message===undefined||res.items[i].last_message===""){
alert("SOMETHING WENT WRONG");
}
else{
console.log("RES.ITEMS: ", res.items);
console.log("RES ITEMS LEN", res.items.length);
console.log("***********************I IS SET TO: ",i," *******************************************");
console.log("RAWR",res.items);
console.log(res.items[i].last_message_date_sent,res.items[i].last_message);
console.log(res.items[i]);
if (res.items[i].last_message === undefined || res.items[i].last_message===null || res.items[i].last_message===""){
console.log("FAIL");
}
else{
var dialogID = res.items[i]._id;
var sent = res.items[i].created_at;
console.log(res.items[i].created_at);
var year = sent.substring(0,4);
var month = sent.substring(5,7);
var day = sent.substring(8,10);
var userIDInChat;
var j =0;
userArray=[];
var userArray = res.items[i].occupants_ids;
console.log("USER ARRAY: ",userArray);
for (j; j<userArray.length; j++){
console.log(userArray[j]);
var testID = window.localStorage.getItem("userID");
console.log("USERARRAY[j]", userArray[j]);
if (userArray[j] != testID){
console.log("INSIDE THE IF STATEMENT");
userIDInChat = userArray[j];
window.localStorage.setItem("userIDInChat", userIDInChat);
console.log("//*******BLOCK ID SET TO********\\", userIDInChat, testID, res);
$.get("http://www.domain.co.uk/API/getUserByID.php", { userID: userIDInChat}, function (data) {
console.log("API CALL:",data);
chatOpponent = data;
console.log(chatOpponent);
console.log("------------------------------------------------------------------------------------------");
renderBlock(res,j,i,chatOpponent,userIDInChat,userArray,testID,day,month,year,dialogID);
});
}
}
}
}
}
//End block
};
function renderBlock(res,j,i,chatOpponent,userIDInChat,userArray,testID,day,month,year,dialogID){
console.log("(res,j,i,chatOpponent,userIDInChat,userArray,testID)");
console.log("RENDERBLOCK PARAMS: ",res,j,i,chatOpponent,userIDInChat,userArray,testID);
//insert function here
console.log("RES: ",res);
var senderID = userIDInChat;
//compare date - vs. moment - today, yesterday or date
sent = day + "/" + month + "/" + year;
console.log(sent);
var onMessages = function(err,result){
window.localStorage.setItem("RESULTTEST",result);
console.log("ONMESSAGESRESULTHERE",err,result);
//console.log("---------onMessages---------",result.items[i].date_sent);s
};
var msgList = QB.chat.message.list({chat_dialog_id: dialogID}, onMessages);
var messages;
console.log(messages);
if (res.items[i].last_message.length>=140) {
var last_message = res.items[i].last_message.substring(0,140)+".....";
}
else{
var last_message = res.items[i].last_message;
}
var readFlag = res.items[i].read;
console.log("SENDERID:", senderID, "username: ", chatOpponent, "last_message", last_message, "sentFlag");
if (readFlag === 1){
var read = "fa-envelope-o";
}
else {
var read = "fa-envelope";
}
var html = "<div class='messageBlock' id='"+senderID+"'><div style='width:10%;min-height:64px;float:left;'><i class='fa '"+read+"'></i><p>"+sent+"</p></div><div style='width:90%;min-height:64px;float:right;'><p class='user'><b><i>"+chatOpponent+"</b></i></p><p>"+last_message+"</p></div></div>";
Object being looped on:
Object {total_entries: 2, skip: 0, limit: 50, items: Array[2]}items: Array[2]0: Object_id: "54e4bd3929108282d4072a37"created_at: "2015-02-18T16:26:33Z"last_message: "test"last_message_date_sent: 1425640757last_message_user_id: 2351789name: nulloccupants_ids: Array[2]photo: nulltype: 3unread_messages_count: 0user_id: 2351781xmpp_room_jid: null__proto__: Object1: Object_id: "54ec990f29108282d40b19e2"created_at: "2015-02-24T15:30:23Z"last_message: "herro!"last_message_date_sent: 1424858692last_message_user_id: 2394026name: nulloccupants_ids: Array[2]photo: nulltype: 3unread_messages_count: 0user_id: 2351789xmpp_room_jid: null__proto__: Objectlength: 2__proto__: Array[0]limit: 50skip: 0total_entries: 2__proto__: Object

for (i;i<=res.items.length;i++){
should be
for (i;i<res.items.length;i++){

Simple you are looping one too many times.
for (i;i<=res.items.length;i++){
^^
arrays are zero index so that means that last index is the length minus one.
for (i;i<res.items.length;i++){
^^

Related

how to remove first 3 word in the sentence In javascript

How can I remove the first three words in java script when the response from the api I want to delete or remove the first three words.. the message from api is like below
"Error: GraphQL error: Account code already taken "
var temp = e.message
var temp1 = temp.map(function(f) {
return f.substring(temp.indexOf(' ') + 2);
});
console.log("Output", temp1)
expected output : " Account code already taken"
var st = "Error: GraphQL error: Account code already taken."
var s = st.split(' ')
s.splice(0,3)
st = s.join(' ');
console.log("Output", st)
Try this code
If the certain word like error: is present in the message then you can split the string with the word and return string from specific index:
var temp = "Error: GraphQL error: Account code already taken "
var temp1 = function(f) {
if(f.split('error:').length > 1)
return f.split('error:')[1].trim();
else return f;
};
console.log("Output:", temp1(temp))
Update: First, I will suggest you to fix the message format if possible. If you have to possible situation where the word in the message could be either error or error: then you can try the following:
var temp = "Error: GraphQL error Account code already taken "
var temp1 = function(f) {
var splitVal = 'error';
if(f.includes('error:'))
splitVal = 'error:';
if(f.split(splitVal).length > 1)
return f.split(splitVal)[1].trim();
else return f;
};
console.log("Output:", temp1(temp));
Create/modify the function to remove one word:
function removeOne(f) {
return f.substring(f.indexOf(' ') + 1);
}
and call it 3 times:
var temp1 = removeOne(removeOne(removeOne(temp)));
or in a loop:
var temp1 = temp;
for(var i = 0; i < 3; i++) {
temp1 = removeOne(temp1);
}
EDIT: wrapped the post into a snippet if someone had doubts:
var temp = "Error: GraphQL error: Account code already taken ";
/* Create/modify the function to remove one word: */
function removeOne(f) {
return f.substring(f.indexOf(' ') + 1);
}
/*and call it 3 times:*/
var temp1 = removeOne(removeOne(removeOne(temp)));
console.log("Output", temp1);
/*or in a loop:*/
temp1 = temp;
for(var i = 0; i < 3; i++) {
temp1 = removeOne(temp1);
}
console.log("Output", temp1);
const shorten = () => input.slice(21, input.length)

Searching two tables in one function in DynamoDB

I am trying to link two tables in DynamoDB for an Amazon Alexa skill. I am using two tables one is named 'yesno' and the other 'fixtures'. The fixtures table has a list of 22 names in each record and these names are in the 'yesno' table along with the column 'goals'. Here you can see the tables in more detail. Name Table:
Fixtures Table:
As you can see there are names that link the two databases together. I use the team1 column to search the fixtures table and use the name column to search the name table. Here is my code for searching:
function readDynamoItem(params2, callback) {
var AWS = require('aws-sdk');
AWS.config.update({region: AWSregion});
var dynamodb = new AWS.DynamoDB();
const names = new Array();
console.log('reading item from DynamoDB table');
dynamodb.scan(params2, function (err, data){
if (err) console.log(err, err.stack); // an error occurred
else{
console.log(data); // successful response
//tried to put a automatic loop for the long bit of code after this but didnt work so anyone with insight on this too would be helpful
/*for(var i = 1; i <= 11; i++){
var str = "T1S";
var pos = i.toString();
pos = str.concat(pos);
names[i] = jsonToString(data.Items[0].pos);
}
for(var j = 1; j <= 11; j++){
str = "T2S";
pos = j.toString();
pos = str.concat(pos);
names[(j+11)] = jsonToString(data.Items[0].pos);
}
*/
names[1] = jsonToString(data.Items[0].T1S1);
names[2] = jsonToString(data.Items[0].T1S2);
names[3] = jsonToString(data.Items[0].T1S3);
names[4] = jsonToString(data.Items[0].T1S4);
names[5] = jsonToString(data.Items[0].T1S5);
names[6] = jsonToString(data.Items[0].T1S6);
names[7] = jsonToString(data.Items[0].T1S7);
names[8] = jsonToString(data.Items[0].T1S8);
names[9] = jsonToString(data.Items[0].T1S9);
names[10] = jsonToString(data.Items[0].T1S10);
names[11] = jsonToString(data.Items[0].T1S11);
names[12] = jsonToString(data.Items[0].T2S1);
names[13] = jsonToString(data.Items[0].T2S2);
names[14] = jsonToString(data.Items[0].T2S3);
names[15] = jsonToString(data.Items[0].T2S4);
names[16] = jsonToString(data.Items[0].T2S5);
names[17] = jsonToString(data.Items[0].T2S6);
names[18] = jsonToString(data.Items[0].T2S7);
names[19] = jsonToString(data.Items[0].T2S8);
names[20] = jsonToString(data.Items[0].T2S9);
names[21] = jsonToString(data.Items[0].T2S10);
names[22] = jsonToString(data.Items[0].T2S11);
}
});
var goals = new Array();
//for loop to be used later when expanding
//for(var i = 1; i <= 22; i++){
var params = {
TableName: 'yesno',
FilterExpression: 'name = :value',
ExpressionAttributeValues: {':value': {"S": names[2]}}
};
dynamodb.scan(params, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else{
console.log(data); // successful response
var temp = jsonToString(data.Items[0].goals);
goals[1] = temp;
}
callback(goals[1]);
});
//}
}
function jsonToString(str){
str = JSON.stringify(str);
str = str.replace('{\"S\":\"', '');
str = str.replace('\"}', '');
return str;
}
I am trying to use the goals array to print each persons goals off but right now it won't even print one persons and instead will print an undefined object of some sort. I'm guessing it just can't search the names table using the names array. The main bit of code I am having a problem with is when searching the yesno table as you can see in this code:
var goals = new Array();
//for loop to be used later when expanding
//for(var i = 1; i <= 22; i++){
var params = {
TableName: 'yesno',
FilterExpression: 'name = :value',
ExpressionAttributeValues: {':value': {"S": names[2]}}
};
dynamodb.scan(params, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else{
console.log(data); // successful response
var temp = jsonToString(data.Items[0].goals);
goals[1] = temp;
}
callback(goals[1]);
});
//}
I know for sure there is nothing wrong with the implementation but here it is just in case it is helpful:
const handlers = {
'LaunchRequest': function () {
this.response.speak('welcome to magic answers. ask me a yes or no question.').listen('try again');
this.emit(':responseReady');
},
'MyIntent': function () {
var MyQuestion = this.event.request.intent.slots.MyQuestion.value;
console.log('MyQuestion : ' + MyQuestion);
const params2 = {
TableName: 'Fixtures',
FilterExpression: 'team1 = :value',
ExpressionAttributeValues: {':value': {"S": MyQuestion.toLowerCase()}}
};
//const params3 = {
// TableName: 'Fixtures',
// FilterExpression: 'team2 = :value',
// ExpressionAttributeValues: {':value': {"S": MyQuestion.toLowerCase()}}
//};
readDynamoItem(params2, myResult=>{
var say = MyQuestion;
say = myResult;
say = 'The top scorer for ' + MyQuestion + ' is ' + myResult;
this.response.speak(say).listen('try again');
this.emit(':responseReady');
});
},
'AMAZON.HelpIntent': function () {
this.response.speak('ask me a yes or no question.').listen('try again');
this.emit(':responseReady');
},
'AMAZON.CancelIntent': function () {
this.response.speak('Goodbye!');
this.emit(':responseReady');
},
'AMAZON.StopIntent': function () {
this.response.speak('Goodbye!');
this.emit(':responseReady');
}
}
;

Java Script array get undefined

when I print the whole array it's print.but if I try to print element by element it's print as undefined.this is my function. I print the arrays at end of the function.client functions are used to connect ajax API.i tried to get integer id that matching to a specific string from database via ajax functions and push them into the two arrays.
function fetch() {
var arrayForClass = [];//this is a array get undefined at the end
var arrayForMessage = [];//this is a array get undefined at the end
exceptionPattern ="";
receivedData.length = 0;
var queryInfo;
var queryForSearchCount = {
tableName: "LOGANALYZER",
searchParams: {
query: "_eventTimeStamp: [" + from + " TO " + to + "]",
}
};
client.searchCount(queryForSearchCount, function (d) {
if (d["status"] === "success" && d["message"] > 0) {
var totalRecordCount = d["message"];
queryInfo = {
tableName: "LOGANALYZER",
searchParams: {
query: "_eventTimeStamp: [" + from + " TO " + to + "]",
start: 0, //starting index of the matching record set
count: totalRecordCount //page size for pagination
}
};
client.search(queryInfo, function (d) {
var obj = JSON.parse(d["message"]);
if (d["status"] === "success") {
for (var i = 0; i < obj.length; i++) {
if(obj[i].values._level === "ERROR" || obj[i].values._level === "WARN"){
receivedData.push([{
date: new Date(parseInt(obj[i].values._eventTimeStamp)).toUTCString(),
level: obj[i].values._level,
class: obj[i].values._class,
content: obj[i].values._content,
trace: (obj[i].values._trace ? obj[i].values._trace : ""),
timestamp: parseInt(obj[i].values._eventTimeStamp)
}]);
}else{
continue;
}
}
console.log(receivedData);
for (forLoopI = 0; forLoopI < receivedData.length; forLoopI++){
var className = receivedData[forLoopI][0].class;
var strclassname = className.toString();
var messageContent = receivedData[forLoopI][0].content;
queryInfo = {
tableName: "EXCEPTION_CLASS_FOR_ERROR_PATTERNS",
searchParams: {
query: "class_name: "+ strclassname + "",
start: 0, //starting index of the matching record set
count: 1 //page size for pagination
}
};
client.search(queryInfo,function(d){
var obj = JSON.parse(d["message"]);
if (d["status"] === "success") {
var num = obj[0].values.id;
var strnum = num.toString();
arrayForClass.push(strnum);
}else{
$(canvasDiv).html(gadgetUtil.getCustemText("No content to display","error while creating the error pattern" +
" please try again"));
}
},function(error){
console.log(error);
error.message = "Internal server error while data indexing.";
onError(error);
});
queryInfo = {
tableName: "ERROR_MESSAGE_CONTENTS",
searchParams: {
query: "message: \""+ messageContent + "\"",
start: 0, //starting index of the matching record set
count: 1 //page size for pagination
}
};
client.search(queryInfo,function(d){
var obj = JSON.parse(d["message"]);
console.log(obj);
if (d["status"] === "success") {
var num2 = obj[0].values.id;
var strnum2 = num2.toString();
arrayForMessage.push(strnum2);
}else{
$(canvasDiv).html(gadgetUtil.getCustemText("No content to display","error while creating the error pattern" +
" please try again"));
}
},function(error){
console.log(error);
error.message = "Internal server error while data indexing.";
onError(error);
});
}
}
}, function (error) {
console.log(error);
error.message = "Internal server error while data indexing.";
onError(error);
});
}else{
$(canvasDiv).html(gadgetUtil.getCustemText("No content to display","there are no error patterns which include this error" +
" please try another one"));
}
}, function (error) {
console.log(error);
error.message = "Internal server error while data indexing.";
onError(error);
});
console.log("------------------");
for (var j = 0; j < 8; j++) {
console.log(arrayForClass[j]);//prints undefine
}
console.log("------------------");
console.log(arrayForClass[0]); //prints undefine
console.log(arrayForClass);//prints corectly
console.log(arrayForMessage);//printd corectly
}
Your API call is asynchronous, which mean it's continue working to the next line even your call is not finished.
You get undefined because your console.log reference to the not exists yet variable. arrayForClass is empty at that moment, so arrayForClass[0] is not exists.
Next line you get correct result because you console.log to an existing variable, even it's empty at the moment, but your debugger tool is trying to be smart by update it for you in the console when your data came in.
if you really want to see the actual value at that point, you need to somehow make it immutable, for example :
console.log(JSON.parse(JSON.stringify(arrayForClass)));
This is only explain why you get data in the console like that.If you need to use those variable, It's has to be done inside those callback function regarding each calls.

How to empty an Array in a Script

I have a script that uses AJAX/PHP/SQL to query data and pushes it into an array with a bunch of IF's statements. The changeData function is called every 6 seconds. The first query I return I have 6 arrays. The second time i send a request, my push array(IsVacant1) is double and went to 12. after a while, I have over 500 arrays going into my .each statement.
How do I 'clear' this every time I make a request so that I am not adding arrays? Any help is most appreciated.
function changeData() {
isPaused = true;
var mydata0 = null;
$.post('php/ProductionChange.php', {
'WC': cc
}, function(data) { // This is Where I use an AJAX call into a php file.
mydata0 = data; // This takes the array from the call and puts it into a variable
var pa = JSON.parse(mydata0); // This parses the data into arrays and elements
var temp = {};
var bayData = '';
if (pa != null) {
for (var i = 0; i <= pa.length - 1; i++) {
var job = pa[i][0];
var shipdate = pa[i][1];
var status = pa[i][2];
var name = pa[i][3];
var EnclLoc = pa[i][13];
var Enclsize = pa[i][14];
var backpan = pa[i][15];
var percentCom = pa[i][16];
var IsVisible = pa[i][17];
var png = pa[i][18];
var WorkC = pa[i][20];
baydata = 'bayData' + i + '';
temp = {
job, shipdate, name, EnclLoc, Enclsize, backpan, percentCom, IsVisible, png, WorkC, status
};
isVacant1.push({
baydata: temp
});
}
} else {
ii = 1;
//alert("There are no more job numbers in this bay location. Thank you. ");
}
$.each(isVacant1, function(key, value) {
var job = value.baydata.job;
var ship = value.baydata.shipdate;
var name = value.baydata.name;
var encl = value.baydata.EnclLoc;
var EnclSize = value.baydata.EnclLoc;
var percentCom = value.baydata.percentCom;
var backpan = value.baydata.backpan;
var PngLogo = value.baydata.png;
var IsVisible = value.baydata.IsVisible;
var WorkC = value.baydata.WorkC;
var status = value.baydata.status;
var p = WorkC;
WorkC = (WorkC < 10) ? ("0" + WorkC) : WorkC;
//// remember if the encl location matches the workcell cell then do stuff based on that....... hint encl image not hiding becase of duplicate 17s
if (((encl == p) || (backpan == p)) && job != 123) {
$('#WC' + p).show();
document.getElementById("bayData" + p).innerHTML = name + ' ' + ship; // Work Cell Name and Ship Date
document.getElementById("bayData" + p + "a").innerHTML = job; // Work cell Job Number
document.getElementById("percentCom" + p).innerHTML = percentCom + '%'; // Work Cell Percent Complete
} else {
$('#WC' + p).hide();
From your question it looks like you want to clear the isVacant1 array.
In your ajax callback just put isVacant1 = []; as the first line. Like this
function(data) { // This is Where I use an AJAX call into a php file.
isVacant1 = [];
mydata0 = data; // This takes the array from the call and puts it into a variable
var pa = JSON.parse(mydata0); // This parses the data into arrays and elements
var temp = {};
var bayData = '';
..................
From your code it's not clear how you are declaring/initializing isVacant1 so i have suggested isVacant1 = [] otherwise you can also use isVacant1.length = 0.
You can also take a look here How do I empty an array in JavaScript?

Wait for Javascript Web Scraping Function to finish before running for next page?

I am attempting to create a web scraper (in node.js) that will pull down information from a site, and write it to a file. I have it built to correctly work for one page, but when I try to use the function in a for loop, to iterate through multiple games, I get bad data in all of the games.
I understand that this is related to Javascript's asynchronous nature, and I have read about callback functions, but I'm not sure I understand how to apply it to my code. Any help would be GREATLY appreciated:
for(x = 4648; x < 4650; x++){ //iterate over a few gameIDs, used in URL for request
scrapeGame(x);
}
function scrapeGame(gameId){
//request from URL, scrape HTML to arrays as necessary
//write final array to file
}
Essentially, what I am looking to do, is within the for loop, tell it to WAIT to finish the scrapeGame(x) function before incrementing x and running it for the next game -- otherwise, the arrays start to overwrite each other and the data becomes a huge mess.
EDIT: I've now included the full code which I am attempting to run! I'm getting errors when looking in the files after they are written. For example, the first file is 8kb, second is ~16, 3rd is ~32, etc. It seems things aren't getting cleared before running the next game.
Idea of the program is to pull Jeopardy questions/answers from the archive site in order to eventually build a quiz app for myself.
//Iterate over arbitrary number of games, scrape each
for(x = 4648; x < 4650; x++){
scrapeGame(x, function(scrapeResult) {
if(scrapeResult){
console.log('Scrape Successful');
} else {
console.log('Scrape ERROR');
}
});
}
function scrapeGame(gameId, callback){
var request = require('request');
cheerio = require('cheerio');
fs = require('fs');
categories = [];
categorylist = [];
ids = [];
clues = [];
values = ['0','$200','$400','$600','$800','$1000','$400','$800','$1200','$1600','$2000'];
valuelist = [];
answers = [];
array = [];
file = [];
status = false;
var showGameURL = 'http://www.j-archive.com/showgame.php?game_id=' + gameId;
var showAnswerURL = 'http://www.j-archive.com/showgameresponses.php?game_id=' + gameId;
request(showGameURL, function(err, resp, body){
if(!err && resp.statusCode === 200){
var $ = cheerio.load(body);
//add a row to categories to avoid starting at 0
categories.push('Category List');
//pull all categories to use for later
$('td.category_name').each(function(){
var category = $(this).text();
categories.push(category);
});
//pull all clue IDs (coordinates), store to 1d array
//pull any id that has "stuck" in the string, to prevent duplicates
$("[id*='stuck']").each(function(){
var id = $(this).attr('id');
id = id.toString();
id = id.substring(0, id.length - 6);
ids.push(id);
//if single J, pick category 1-6
if (id.indexOf("_J_") !== -1){
var catid = id.charAt(7);
categorylist.push(categories[catid]);
var valId = id.charAt(9);
valuelist.push(values[valId]);
}
//if double J, pick category 7-12
else if (id.indexOf("_DJ_") !== -1){
var catid = parseInt(id.charAt(8)) + 6;
categorylist.push(categories[catid]);
var valId = parseInt(id.charAt(10)) + 5;
valuelist.push(values[valId]);
}
//if final J, pick category 13
else {
categorylist.push(categories[13]);
}
});
//pull all clue texts, store to 1d array
$('td.clue_text').each(function(){
var clue = $(this).text();
clues.push(clue);
});
//push pulled values to big array
array.push(ids);
array.push(categorylist);
array.push(valuelist);
array.push(clues);
//new request to different URL to pull responses
request(showAnswerURL, function(err, resp, body){
if(!err && resp.statusCode === 200){
var $ = cheerio.load(body);
$('.correct_response').each(function(){
var answer = $(this).text();
answers.push(answer);
});
//push answers to big array
array.push(answers);
//combine arrays into 1-d array to prep for writing to file
for(var i = 0; i < array[0].length; i++){
var print = array[0][i] + "|" + array[1][i] + "|" + array[2][i] + "|" + array[3][i] + "|" + array[4][i];
var stringPrint = print.toString();
file.push(stringPrint);
}
//update string, add newlines, etc.
var stringFile = JSON.stringify(file);
stringFile = stringFile.split('\\').join('');
stringFile = stringFile.split('","').join('\n');
//write to file, eventually will append to end of one big file
fs.writeFile('J_GAME_' + gameId +'.txt', stringFile, function(err) {
if(err) {
console.log(err);
} else {
console.log("Game #" + gameId + " has been scraped.");
status = true;
}
});
}
});
}
});
//clear arrays used
valuelist = [];
answers = [];
categories = [];
categorylist = [];
ids = [];
clues = [];
array = [];
file = [];
//feed callback status
callback(status);
}
// Iterate over a few gameIDs, used in URL for request.
for (x = 4648; x < 4650; x++) {
// Pass in the callback as an anonymous function.
// So below I am passing in the id and the function I want to execute.
// AND, defining the results I am expecting as passed in arguments.
scrapeGame(x, function(scrapeResult, err) {
// This will *NOT* execute *UNTIL* you call it in the function below.
// That means that the for loop's execution is halted.
// This function receives the status that is passed in,
// in this case, a boolean true/false and an error if any.
if (scrapeResult) {
// Scrape was true, nothing to do.
// The for loop will now move on to the next iteration.
console.log('Scrape Successful');
} else {
// Scrape was false, output error to console.log and
// break loop to handle error.
console.log('Scrape ERROR :: ' + err);
// Notice we are calling break while in the
// scope of the callback function
// Remove the break if you want to just move onto
// the next game ID and not stop the loop
break;
}
});
}
// This function now accepts two arguments.
function scrapeGame(gameId, callback) {
// ************************************************
// ** Do Your Work Here **
// Request from URL, scrape HTML to arrays as necessary.
// Write final array to file.
// After file creation, execute the callback and pass bool
// status (true/false).
// ************************************************
var request = require('request'),
cheerio = require('cheerio'),
fs = require('fs'),
categories = [],
categorylist = [],
ids = [],
clues = [],
values = [
'0',
'$200',
'$400',
'$600',
'$800',
'$1000',
'$400',
'$800',
'$1200',
'$1600',
'$2000'
],
valuelist = [],
answers = [],
array = [],
file = [],
showGameURL = 'http://www.j-archive.com/showgame.php?game_id=' + gameId,
showAnswerURL = 'http://www.j-archive.com/showgameresponses.php?game_id=' + gameId;
request(showGameURL, function(err, resp, body) {
if (!err && resp.statusCode === 200) {
var $ = cheerio.load(body);
//add a row to categories to avoid starting at 0
categories.push('Category List');
//pull all categories to use for later
$('td.category_name').each(function() {
var category = $(this).text();
categories.push(category);
});
//pull all clue IDs (coordinates), store to 1d array
//pull any id that has "stuck" in the string, to prevent duplicates
$("[id*='stuck']").each(function() {
var id = $(this).attr('id');
id = id.toString();
id = id.substring(0, id.length - 6);
ids.push(id);
//if single J, pick category 1-6
if (id.indexOf("_J_") !== -1) {
var catid = id.charAt(7);
categorylist.push(categories[catid]);
var valId = id.charAt(9);
valuelist.push(values[valId]);
}
//if double J, pick category 7-12
else if (id.indexOf("_DJ_") !== -1) {
var catid = parseInt(id.charAt(8)) + 6;
categorylist.push(categories[catid]);
var valId = parseInt(id.charAt(10)) + 5;
valuelist.push(values[valId]);
}
//if final J, pick category 13
else {
categorylist.push(categories[13]);
}
});
//pull all clue texts, store to 1d array
$('td.clue_text').each(function() {
var clue = $(this).text();
clues.push(clue);
});
//push pulled values to big array
array.push(ids);
array.push(categorylist);
array.push(valuelist);
array.push(clues);
//new request to different URL to pull responses
request(showAnswerURL, function(err, resp, body) {
if (!err && resp.statusCode === 200) {
var $ = cheerio.load(body);
$('.correct_response').each(function() {
var answer = $(this).text();
answers.push(answer);
});
//push answers to big array
array.push(answers);
//combine arrays into 1-d array to prep for writing to file
for (var i = 0; i < array[0].length; i++) {
var print = array[0][i] + "|" + array[1][i] + "|" + array[2][i] + "|" + array[3][i] + "|" + array[4][i];
var stringPrint = print.toString();
file.push(stringPrint);
}
//update string, add newlines, etc.
var stringFile = JSON.stringify(file);
stringFile = stringFile.split('\\').join('');
stringFile = stringFile.split('","').join('\n');
//write to file, eventually will append to end of one big file
fs.writeFile('J_GAME_' + gameId + '.txt', stringFile, function(err) {
//clear arrays used
valuelist = [];
answers = [];
categories = [];
categorylist = [];
ids = [];
clues = [];
array = [];
file = [];
if (err) {
// ******************************************
// Callback false with error.
callback(false, err);
// ******************************************
} else {
console.log("Game #" + gameId + " has been scraped.");
// ******************************************
// Callback true with no error.
callback(true);
// ******************************************
}
});
}
});
}
});
}
My assumption is that you want them to be scraped one after one, not in parallel. So, for loop won't help. The following approach should do the trick:
var x = 4648;
var myFunc = scrapeGame(x, function cb(){
if(x >= 4650){
return;
}
x++;
return myFunc(x, cb);
});
function scrapeGame(gameId){
//request from URL, scrape HTML to arrays as necessary
//write final array to file
}
For nested async function, where you want them be executed in serial manner, you should just forget about for loop.
An example of correct request handling with http client:
function scrapeGame(gameId, cb){
//your code and set options
http.request(options, function(response){
var result = "";
response.on('data', function (chunk) {
result += chunk;
});
response.on('end',function(){
//write data here;
//do the callback
cb();
});
});
}
I solved the ROOT cause of the issue that I was seeing, though I do believe without the callback assistance from red above, I would have been just as lost.
Turns out the data was processing correctly, but the file write was scrambling. Turns out that there is a different method to call instead of writeFile or appendFile:
fs.appendFileSync();
Calling the Synchronous version processed the writes to the file IN THE ORDER they got appended to the file, instead of just going for it. This, in addition to the callback help above, solved the issue.
Thanks to everyone for the assistance!

Categories

Resources