invalid non-string/buffer chunk Node.js - javascript

I'm studying Node.js in college, and it's my first time learning this kind of programming language. I've got some errors in my attempt to build a chat server. When I try to connect one client to the server, the connection closes immediately and I get the error Invalid non-string/buffer chunk. I uploaded a screenshot for you to check out what is wrong, because I've been thinking about it for a while and I can't find any solution.
My Javascript code:
var net = require('net');
var s = require
var sockets = [];
var nombres = [];
var nombresUsados = [];
console.log("Se ha iniciado el sevidor");
var server = net.createServer(function(socket) {
socket.push(socket);
nombres.push("Cliente:" + sockets.indexOf(socket));
nombresUsados.push("Cliente:" + socket.indexOf(socket));
console.log("Cliente aceptado, nick:" + nombres[sockets.indexOf(socket)]);
socket.write("Bienvenido" + nombres[sockets.indexOf(socket)] + "\n");
socket.on('data', function(d) {
var entrada = d.toString();
var UsuarioUnico = entrada.match(/^msg/);
var cambiarNick = entrada.match(/^nick/);
var quit = entrada.match(/^quit/);
if (cambiarNick == "nick") {
var repetido = 0;
var nombresSinNick = entrada.replace(cambiarNick, '');
for (var i = nombres.length - 1; i <= 0; i--) {
if (nombresSinNick.substring(0, nombres[i].toString().length) == nombres[i].toString()) {
socket.write("KO, escoja otro nombre\n")
repetido = 1;
}
};
if (repetido == 0) {
nombres[sockets.indexOf(socket)] == nombresSinNick.trim();
process.on('uncaughtException', function(err) {
socket.write("KO\n");
});
socket.write("OK. " + nombres[sockets.indexOf(socket)] + "\n");
console.log(nombresUsados[sockets.indexOf(socket)]) + "su nombre ha sido cambiado por:" + nombres[sockets.indexOf(socket)];
nombresUsados[sockets.indexOf(socket)] = nombresSinNick.trim();
}
} else if (UsuarioUnico = "msg") {
var nombresSinMsg = entrada.replace(UsuarioUnico, '');
var encontrado = 0;
for (var i = nombres.length - 1; i <= 0; i--) {
if (nombresSinMsg.substring(0, nombres[i].toString().length) == nombres[i].toString()) {
var mensaje = nombresSinMsg.replace(nombres[i], '');
}
};
socket.on('end', function() { // CALLBACK: desconexión de cliente
if (quit == 'quit') {
var i = nombres[sockets.indexOf(socket)];
sockets.splice(i, 1);
console.log("Ha salido el usuario:" + nombres[sockets.indexOf(socket)]);
}
});
}
});
});
server.listen(9000);

I think the problem is the line socket.push(socket). Probably you mean sockets.push(socket). What you're doing now, is attempting to push the socket instance into the socket stream which fails because, as the error says, it's not a string or buffer.

Related

google docs apps script function calling very slow

I'm writing a google docs apps script in making a google docs add-on. When the user clicks a button in the sidebar, an apps script function is called named executeSpellChecking. This apps script function makes a remote POST call after getting the document's text.
total time = time that takes from when user clicks the button, until the .withSuccessHandler(, that means until executeSpellChecking returns = 2000 ms
function time = time that takes for the executeSpellChecking call to complete from its start to its end = 1400 ms
t3 = time that takes for the remote POST call to be completed = 800ms
t4 = time that takes for the same remote POST call to complete in a VB.NET app = 200ms
Problems:
Why total time to complete is bigger than total function time by a staggering 600ms, what else happens there? shouldn't they be equal? How can I improve it?
Why t3 is bigger than t4 ? Shouldn't they be equal? Is there something wrong with POST requests when happening from .gs? How can I improve it ?
the code is (sidebar.html):
function runSpellChecking() {
gb_IsSpellcheckingRunning = true;
//gb_isAutoCorrecting = false;
gi_CorrectionCurrWordIndex = -1;
$("#btnStartCorr").attr("disabled", true);
$("#divMistakes").html("");
this.disabled = true;
//$('#error').remove();
var origin = $('input[name=origin]:checked').val();
var dest = $('input[name=dest]:checked').val();
var savePrefs = $('#save-prefs').is(':checked');
//var t1 = new Date().getTime();
console.time("total time");
google.script.run
.withSuccessHandler(
function(textAndTranslation, element) {
if (gb_IsSpellCheckingEnabled) {
console.timeEnd("total time");
//var t2 = new Date().getTime();
go_TextAndTranslation = JSON.parse(JSON.stringify(textAndTranslation));
var pagewords = textAndTranslation.pagewords;
var spellchecked = textAndTranslation.spellchecked;
//alert("total time to complete:" + (t2-t1) + "###" + go_TextAndTranslation.time);
//irrelevant code follows below...
}
})
.withFailureHandler(
function(msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
})
.withUserObject(this)
.executeSpellChecking(origin, dest, savePrefs);
}
and the called function code is (spellcheck.gs):
function executeSpellChecking(origin, dest, savePrefs) {
//var t1 = new Date().getTime();
console.time("function time");
var body = DocumentApp.getActiveDocument().getBody();
var alltext = body.getText();
var lastchar = alltext.slice(-1);
if (lastchar != " " && lastchar != "\n") {
body.editAsText().insertText(alltext.length, "\n");
alltext = body.getText();
}
var arr_alltext = alltext.split(/[\s\n]/);
var pagewords = new Object;
var pagewordsOrig = new Object;
var pagewordsOrigOffset = new Object;
var offset = 0;
var curWord = "";
var cnt = 0;
for (var i = 0; i < arr_alltext.length; i++) {
curWord = arr_alltext[i];
if (StringHasSimeioStiksis(curWord)) {
curWord = replaceSimeiaStiksis(curWord);
var arr3 = curWord.split(" ");
for (var k = 0; k < arr3.length; k++) {
curWord = arr3[k];
pagewords["" + (cnt+1).toString()] = curWord.replace(/[`~##$%^&*()_|+\-="<>\{\}\[\]\\\/]/gi, '');
pagewordsOrig["" + (cnt+1).toString()] = curWord;
pagewordsOrigOffset["" + (cnt+1).toString()] = offset;
offset += curWord.length;
cnt++;
}
offset++;
} else {
pagewords["" + (cnt+1).toString()] = curWord.replace(/[`~##$%^&*()_|+\-="<>\{\}\[\]\\\/\n]/gi, '');
pagewordsOrig["" + (cnt+1).toString()] = curWord;
pagewordsOrigOffset["" + (cnt+1).toString()] = offset;
offset += curWord.length + 1;
cnt++;
}
}
var respTString = "";
var url = 'https://www.example.org/spellchecker.php';
var data = {
"Text" : JSON.stringify(pagewords),
"idOffset" : "0",
"lexID" : "8",
"userEmail" : "test#example.org"
};
var payload = JSON.stringify(data);
var options = {
"method" : "POST",
"contentType" : "application/json",
"payload" : payload
};
//var t11 = new Date().getTime();
console.time("POST time");
var response = UrlFetchApp.fetch(url, options);
console.timeEnd("POST time");
//var t22 = new Date().getTime();
var resp = response.getContentText();
respTString = resp;
var spellchecked = JSON.parse(respTString);
var style = {};
for (var k in pagewords){
if (pagewords.hasOwnProperty(k)) {
if (spellchecked.hasOwnProperty(k)) {
if (spellchecked[k].substr(0, 1) == "1") {
style[DocumentApp.Attribute.FOREGROUND_COLOR] = "#000000";
}
if (spellchecked[k].substr(0, 1) == "0") {
style[DocumentApp.Attribute.FOREGROUND_COLOR] = "#FF0000";
}
if (spellchecked[k].substr(0, 1) == "4") {
style[DocumentApp.Attribute.FOREGROUND_COLOR] = "#0000FF";
}
if (pagewordsOrigOffset[k] < alltext.length) {
body.editAsText().setAttributes(pagewordsOrigOffset[k], pagewordsOrigOffset[k] + pagewordsOrig[k].length, style);
}
}
}
}
//var t2 = new Date().getTime();
console.timeEnd("function time")
return {
"pagewords" : pagewords,
"pagewordsOrig" : pagewordsOrig,
"pagewordsOrigOffset" : pagewordsOrigOffset,
"spellchecked" : spellchecked
}
}
Thank you in advance for any help.
EDIT: I updated the code to use console.time according to the suggestion, the results are:
total time: 2048.001953125 ms
Jun 21, 2021, 3:01:40 PM Debug POST time: 809ms
Jun 21, 2021, 3:01:41 PM Debug function time: 1408ms
So the problem is not how time is measured. function time is 1400ms, while the time it takes to return is 2000ms, a difference of 600ms and the POST time is a staggering 800ms, instead of 200ms it takes in VB.net to make the exact same POST call.
Use console.time() and console.timeEnd():
https://developers.google.com/apps-script/reference/base/console
I modified the code for you. console.timeEnd() outputs the time duration in the console automatically, so I removed the alert for you that showed the time difference.
You might want the strings that I used as the parameter as some sort of constant variable, so there are no magic strings used twice. I hope this is of use to you.
function runSpellChecking() {
gb_IsSpellcheckingRunning = true;
//gb_isAutoCorrecting = false;
gi_CorrectionCurrWordIndex = -1;
$("#btnStartCorr").attr("disabled", true);
$("#divMistakes").html("");
this.disabled = true;
//$('#error').remove();
var origin = $('input[name=origin]:checked').val();
var dest = $('input[name=dest]:checked').val();
var savePrefs = $('#save-prefs').is(':checked');
console.time("total time");
google.script.run
.withSuccessHandler(
function(textAndTranslation, element) {
if (gb_IsSpellCheckingEnabled) {
console.timeEnd("total time");
go_TextAndTranslation = JSON.parse(JSON.stringify(textAndTranslation));
var pagewords = textAndTranslation.pagewords;
var spellchecked = textAndTranslation.spellchecked;
//irrelevant code follows below...
}
})
.withFailureHandler(
function(msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
})
.withUserObject(this)
.executeSpellChecking(origin, dest, savePrefs);
}
function executeSpellChecking(origin, dest, savePrefs) {
console.time("function time");
var body = DocumentApp.getActiveDocument().getBody();
var alltext = body.getText();
var lastchar = alltext.slice(-1);
if (lastchar != " " && lastchar != "\n") {
body.editAsText().insertText(alltext.length, "\n");
alltext = body.getText();
}
var arr_alltext = alltext.split(/[\s\n]/);
var pagewords = new Object;
var pagewordsOrig = new Object;
var pagewordsOrigOffset = new Object;
var offset = 0;
var curWord = "";
var cnt = 0;
for (var i = 0; i < arr_alltext.length; i++) {
curWord = arr_alltext[i];
if (StringHasSimeioStiksis(curWord)) {
curWord = replaceSimeiaStiksis(curWord);
var arr3 = curWord.split(" ");
for (var k = 0; k < arr3.length; k++) {
curWord = arr3[k];
pagewords["" + (cnt+1).toString()] = curWord.replace(/[`~##$%^&*()_|+\-="<>\{\}\[\]\\\/]/gi, '');
pagewordsOrig["" + (cnt+1).toString()] = curWord;
pagewordsOrigOffset["" + (cnt+1).toString()] = offset;
offset += curWord.length;
cnt++;
}
offset++;
} else {
pagewords["" + (cnt+1).toString()] = curWord.replace(/[`~##$%^&*()_|+\-="<>\{\}\[\]\\\/\n]/gi, '');
pagewordsOrig["" + (cnt+1).toString()] = curWord;
pagewordsOrigOffset["" + (cnt+1).toString()] = offset;
offset += curWord.length + 1;
cnt++;
}
}
var respTString = "";
var url = 'https://www.example.org/spellchecker.php';
var data = {
"Text" : JSON.stringify(pagewords),
"idOffset" : "0",
"lexID" : "8",
"userEmail" : "test#example.org"
};
var payload = JSON.stringify(data);
var options = {
"method" : "POST",
"contentType" : "application/json",
"payload" : payload
};
console.time("POST time");
var response = UrlFetchApp.fetch(url, options);
console.timeEnd("POST time");
var resp = response.getContentText();
respTString = resp;
var spellchecked = JSON.parse(respTString);
var style = {};
for (var k in pagewords){
if (pagewords.hasOwnProperty(k)) {
if (spellchecked.hasOwnProperty(k)) {
if (spellchecked[k].substr(0, 1) == "1") {
style[DocumentApp.Attribute.FOREGROUND_COLOR] = "#000000";
}
if (spellchecked[k].substr(0, 1) == "0") {
style[DocumentApp.Attribute.FOREGROUND_COLOR] = "#FF0000";
}
if (spellchecked[k].substr(0, 1) == "4") {
style[DocumentApp.Attribute.FOREGROUND_COLOR] = "#0000FF";
}
if (pagewordsOrigOffset[k] < alltext.length) {
body.editAsText().setAttributes(pagewordsOrigOffset[k], pagewordsOrigOffset[k] + pagewordsOrig[k].length, style);
}
}
}
}
console.timeEnd("function time");
return {
"pagewords" : pagewords,
"pagewordsOrig" : pagewordsOrig,
"pagewordsOrigOffset" : pagewordsOrigOffset,
"spellchecked" : spellchecked
}
}

Comparing JPG files with Photoshop Layers

Is it possible to compare filenames for a set of files that are imported as Photoshop layers ?
I have a folder of 50 jpg images which I have used in a PSD file.
Now I want to check whether all the JPG files are used or not ?
Is it possible to do so ?
As I've said, Photoshop scripting can help you achieve this by using File Objects and basic javascript knowledge. I've modified my old script as you've desired and now it should work well with any nested groups and images.
I highly encourage you to learn scripting and ask questions here wherever you feels confused.
Save below code as 'Script.jsx' and run it from 'File > Scripts > Browse'
Update 2 : Now it saves log.txt file too as per you requested. P.S. Learn from this script and tweak it to your desired result.
// Managing Document
var docs = app.documents;
// Progress Bar
var win = new Window("window{text:'Progress',bounds:[100,100,400,150],bar:Progressbar{bounds:[20,20,280,31] , value:0,maxvalue:100}};");
// assigning activeDocument
if (docs.length != 0) {
var docRef = app.activeDocument;
// Defining the folder
alert("You will be prompted for the folder containing your images.\n" +
"Files will be selected with a '.png'/'.jpg/.jpeg' on the end in the same folder.");
var folder = Folder.selectDialog();
if (!folder) {
exit;
}
var photoFiles = folder.getFiles(/\.(jpg|jpeg|png)$/i);
var matchFiles = [];
var photoFilesName = [];
//Searching for used images
var increment = parseFloat(0);
var divider = parseFloat(100/photoFiles.length);
win.show();
for (var i = 0; i < photoFiles.length; i++) {
increment = increment + divider;
var indexPhotoName = removeExtension(photoFiles[i].displayName);
photoFilesName.push(indexPhotoName);
var doc = activeDocument;
var curLayer;
goThroughLayers(doc, indexPhotoName);
}
function goThroughLayers(parentLayer, targetName) {
for (var i = 0; i < parentLayer.layers.length; i++) {
curLayer = parentLayer.layers[i];
doc.activeLayer = curLayer;
if (curLayer.typename == 'LayerSet') {
goThroughLayers(curLayer, targetName)
} else {
if (curLayer.name == targetName) {
// if (curLayer.name.match(/[e]/ig)) {
matchFiles.push(targetName);
// }
} //end if
} //end else
} //end loop
} //end function
function arr_diff(a1, a2) {
var a = [],
diff = [];
for (var i = 0; i < a1.length; i++) {
a[a1[i]] = true;
}
for (var i = 0; i < a2.length; i++) {
if (a[a2[i]]) {
delete a[a2[i]];
} else {
a[a2[i]] = true;
}
}
for (var k in a) {
diff.push(k);
}
return diff;
}
function removeExtension(str) {
return str.split('.').slice(0, -1).join('.');
}
var missItems = arr_diff(matchFiles, photoFilesName);
if (missItems.length > 0) {
var missFolder = new Folder(photoFiles[0].path + '/Missed%20Files');
if(!missFolder.exists){
missFolder.create();
}
for (var y = 0; y < photoFiles.length; y++) {
var photoTrimName = removeExtension(photoFiles[y].displayName);
for( var x = 0; x < missItems.length ; x++){
if(photoTrimName == missItems[x]){
photoFiles[y].copy(new File(missFolder+'/'+photoFiles[y].displayName));
}
}
};
win.close();
alert("You've missed total " + missItems.length + " files. Press OK to open folder containing missing files. Log report is generated wherever PSD is saved.");
var FileStr = "";
for(var m=0; m<missItems.length; m++){
FileStr = FileStr + '\n' + (m+1) + '. ' + missItems[m];
}
var str = "Your missed files are : " + FileStr;
saveTxt(str);
missFolder.execute();
} else {
win.close();
saveTxt('All Photos are used');
alert('All Photos are used');
}
} else {
alert('Open atleast one document');
}
function saveTxt(txt)
{
var Name = "LogReport_" + app.activeDocument.name.replace(/\.[^\.]+$/, '');
var Ext = decodeURI(app.activeDocument.name).replace(/^.*\./,'');
if (Ext.toLowerCase() != 'psd')
return;
var Path = app.activeDocument.path;
var saveFile = File(Path + "/" + Name +".txt");
if(saveFile.exists)
saveFile.remove();
saveFile.encoding = "UTF8";
saveFile.open("e", "TEXT", "????");
saveFile.writeln(txt);
saveFile.close();
}
In Javascript, it is possible to get some information related to PSD file layers using PSD.js library

What is the correct way to send large CSV Files to Server using jquery

I have a CSV file with ~20,000 records. I send each line using the $.post method to my server using the FileReader API.
The problem is that the browser is buffering each record before starting to send the data and this way is very slow. I want to send each line separately to show a progressbar where it counts the request number of each line.
As this solution is very slow I'm thinking there are must be other ways of doing this to make it faster. Many thanks to your ideas.
$("#form_file").change(function(e) {
if (e.target.files != undefined) {
var reader = new FileReader();
reader.onload = function(e) {
var rows = e.target.result.split("\n");
var index = rows[0];
index = index.split(";");
gesamt = rows.length - 1;
for (var i = 1; i < rows.length; i++) {
var row = rows[i];
cells = row.split(";");
var dataset = {};
for (var ii = 0; ii < cells.length; ii++) {
var value = cells[ii];
var key = index[ii]
var printError = function(error, explicit) {
console.log(`[${explicit ? 'EXPLICIT' : 'INEXPLICIT'}] ${error.name}: ${error.message}`);
}
try {
dataset[key] = value;
} catch (e) {
if (e instanceof RangeError) {
if (e.message.toLowerCase().indexOf('invalid array') !== -1) {
printError(e, true);
} else {
printError(e, false);
}
} else {
printError(e, false);
}
}
}
console.log(dataset);
row = insertrow(dataset, i);
$('#progressbar').show();
$('#progressvalue').text(i + '/' + gesamt);
$('#progresstitle').text('(' + dataset.title + ')');
}
};
var test = reader.readAsText(e.target.files.item(0));
}
});
function insertrow(mydata, step) {
var token = "{{app.request.query.get('_token')}}";
mydata = JSON.stringify(mydata);
$.post('preferences/upload?_token=' + token, {
data: mydata
}, function(data) {
$('#info').show();
var html = data.message + '<br />';
$('#info').append(html);
}, "json");
}

Stop iterating and return true when a match is found between two arrays

I have two datasets, one in indexedDB and one in PouchDB (yes I know PouchDB is an implementation of indexedDB).
The one in indexedDB is a list of rooms, stored in indexedDB via a previous page, and displayed on the current page.
The one in PouchDB is the log of room usage recorded by a room auditor. I want to iterate through the first list, and check if each item appears in the list of audited rooms. If it does appear, I want to set a flag to indicate that.
Here's my Javascript. (I have run this in a browser and it does return true at some point in the process, because the console log output shows that, but the flag doesn't get set against the list item.)
I am wondering if it continues to loop through the audit records and overwrites the "true" value?
this is the function that queries indexedDB and calls the function that queries PouchDB:
function getRoomsInRoute() {
var routeNumber = $.jStorage.get('currentRoute', '');
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;
openRequest = window.indexedDB.open("rooms", 1);
openRequest.onupgradeneeded = function() {
var db = openRequest.result;
var itemStore = db.createObjectStore("rooms", {keyPath: "room_id"});
var index = itemStore.createIndex("rooms", ["route_number"]);
};
openRequest.onerror = function(event) {
console.error(event);
};
openRequest.onsuccess = function (event) {
var db = openRequest.result;
db.onerror = function(event) {
// Generic error handler for all errors targeted at this database's requests
console.error(event.target);
console.log("Database error: " + event.target.error.name || event.target.error || event.target.errorCode);
};
var transaction = db.transaction(['rooms'], "readwrite");
var itemStore = transaction.objectStore("rooms");
var index = itemStore.index("rooms", ["route_number"]);
console.log('DB opened');
var intRouteNumber = parseInt(routeNumber);
//var range = IDBKeyRange.only(intRouteNumber);
itemStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if(cursor) {
var audited;
if(cursor.value.route_number == routeNumber) {
if (checkIfAudited(cursor.value.room_seq)) {
var audited = ' <span class="checked"><i class="fa fa-check"></i></span>';
} else {
var audited = "";
}
$('#mylist').append("<li id="+ cursor.value.room_id +" rel="+ cursor.value.room_seq +"> " + '<small>'+ cursor.value.room_seq + '. </small>' + cursor.value.room_name + ''+audited+'</li> ');
}
cursor.continue();
} else {
console.log('Entries all displayed.');
if(!($.jStorage.get('reverseroute', ''))) {
reverseroute = 'asc';
} else {
reverseroute = $.jStorage.get('reverseroute', '');
}
appendHref(reverseroute);
}
};
// Close the db when the transaction is done
transaction.oncomplete = function() {
db.close();
};
};
}
and this is the function that queries PouchDB to see if it has been audited:
function checkIfAudited(roomseq) {
var today = new Date();
if(is_BST(today) == true) {
var currentHour = today.getHours()+1;
} else {
var currentHour = today.getHours();
}
var currentDay = today.getDate();
var currentMonth = today.getMonth();
options = {},
that = this,
pdb = new PouchDB('pouchroomusage');
options.include_docs = true;
var pouchOpts = {
skipSetup: true
};
var opts = {live: true};
pdb.allDocs(options, function (error, response) {
response.rows.some(function(row){
var auditTime = new Date(row.doc.timestamp);
var auditHour = auditTime.getUTCHours();
var auditDay = auditTime.getDate();
var auditMonth = auditTime.getMonth();
if(row.doc.sequence == roomseq && currentHour == auditHour && currentDay == auditDay && currentMonth == auditMonth) {
var isAudited = true;
console.log('RoomSeq: ' + roomseq + '; auditHour: ' + auditHour + '; currentHour: ' + currentHour + '; auditDay: ' + auditDay);
console.log('currentDay: ' + currentDay + '; auditMonth: ' + auditMonth + '; currentMonth: ' + currentMonth + '; isAudited: ' + isAudited);
} else {
var isAudited = false;
console.log('No matches');
}
return isAudited;
});
});
}
I have read a number of other questions and answers about comparing two arrays.
I don't know how to use a for loop with pdb.allDocs though :(
Here is the output from console.log:
49 No matches
RoomSeq: 1; auditHour: 14; currentHour: 14; auditDay: 16
currentDay: 16; auditMonth: 0; currentMonth: 0; isAudited: true
2300 No matches
So how do I get the second function to stop and return true when it hits a matching record in PouchDB?
First, I wouldn't get too fancy with exploiting the short-circuiting behavior of Array.prototype.some. Use the native functionality available to you. indexedDB provides a built in way to stop advancing a cursor, or load only a limited number of objects from a store.
Second, you probably want to avoid loading all objects from a store when you are only interested a in a few of those objects. Use a cursor to walk the store. Since you appear to want to stop iterating when meeting some condition, simply do not call cursor.continue at that point.
Third, is that even if decide to load all objects from the store first, it would be far better to use a for loop than exploit some. By exploit I mean use the function in a way other than it was intended. I bet that if you revert to using a for loop with a break statement, the code will be clearer and therefore it will be simpler to understand why the loop does not break when you expect it to do so.
Fourth, is that I would take the time to append results of an indexedDB query to an intermediate data structure, like an array, rather than immediately interacting with the DOM. It will separate things more, and you will find the code simpler to debug.
Fifth is that you should be very careful when mixing async calls together with indexedDB. indexedDB is known to have problems when you interleave calls to other promises.
It seems to me that the pouchdb method alldocs is asynchronous.
But you test audition in a synchronous way. Therefore whatever the pdb.alldocs callback function returns will be returned after the checkIfAudited is already returned. Therefore checkIfAudited always returns undefined.
In my opinion, you should create the pouchdb instance only once in temStore.openCursor().onsuccess. Then you need to properly return audit state in checkIfAudited function.
For example, you could do something like the following:
itemStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
if (cursor.value.route_number == routeNumber) {
var audited;
options = {},
pdb = new PouchDB('pouchroomusage');
options.include_docs = true;
pdb.allDocs(options, function (error, allDocsResponse) {
if (checkIfAudited(allDocsResponse, cursor.value.room_seq)) audited = ' <span class="checked"><i class="fa fa-check"></i></span>'
else audited = "";
$('#mylist').append("<li id="+ cursor.value.room_id +" rel="+ cursor.value.room_seq +"> " + '<small>'+ cursor.value.room_seq + '. </small>' + cursor.value.room_name + ''+audited+'</li> ');
});
};
cursor.continue();
} else {
console.log('Entries all displayed.');
if(!($.jStorage.get('reverseroute', ''))) reverseroute = 'asc'
else reverseroute = $.jStorage.get('reverseroute', '');
appendHref(reverseroute);
};
};
And for checkIfAudited:
function checkIfAudited(allDocs, roomseq) {
var today = new Date();
if(is_BST(today) == true) {
var currentHour = today.getHours()+1;
} else {
var currentHour = today.getHours();
}
var currentDay = today.getDate();
var currentMonth = today.getMonth();
for (i=0; i<allDocs.rows.length; i++) {
var row = allDocs.rows[i];
var auditTime = new Date(row.doc.timestamp);
var auditHour = auditTime.getUTCHours();
var auditDay = auditTime.getDate();
var auditMonth = auditTime.getMonth();
if(row.doc.sequence == roomseq && currentHour == auditHour && currentDay == auditDay && currentMonth == auditMonth) {
console.log('RoomSeq: ' + roomseq + '; auditHour: ' + auditHour + '; currentHour: ' + currentHour + '; auditDay: ' + auditDay);
console.log('currentDay: ' + currentDay + '; auditMonth: ' + auditMonth + '; currentMonth: ' + currentMonth + '; isAudited: ' + isAudited);
return true; ///// <---- return that it is audited
} else {
console.log('No matches');
};
});
return false ///// <---- return false if no iteration has matched
}
This is what I ended up with after much tweaking.
Now it adds <span class="checked"><i class="fa fa-check"></i></span> more than once per <li> but at least it adds to every <li>.
So I did a little hack to the CSS to only display one span.checked:
.checked {
color: #fff;
background-color: #006338;
border-radius: 50%;
padding: 2px 3px;
}
/* only display the first instance of checked */
li > span.checked ~ .checked {
display: none;
}
(I also found and fixed another error in my script, where I wasn't setting route_number or room_id.)
function getRoomsInRoute() {
var routeNumber = $.jStorage.get('currentRoute', '');
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;
openRequest = window.indexedDB.open("rooms", 1);
openRequest.onupgradeneeded = function() {
var db = openRequest.result;
var itemStore = db.createObjectStore("rooms", {keyPath: "room_id"});
var index = itemStore.createIndex("rooms", ["route_number"]);
};
openRequest.onerror = function(event) {
console.error(event);
};
openRequest.onsuccess = function (event) {
var db = openRequest.result;
db.onerror = function(event) {
// Generic error handler for all errors targeted at this database's requests
console.error(event.target);
console.log("Database error: " + event.target.error.name || event.target.error || event.target.errorCode);
};
var transaction = db.transaction(['rooms'], "readwrite");
var itemStore = transaction.objectStore("rooms");
var index = itemStore.index("rooms", ["route_number"]);
console.log('DB opened');
var intRouteNumber = parseInt(routeNumber);
//var range = IDBKeyRange.only(intRouteNumber);
itemStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
var audited = "";
if(cursor) {
if(cursor.value.route_number == routeNumber) {
$('#mylist').append("<li id="+ cursor.value.room_id +" rel="+ cursor.value.room_seq +"> " + '<small>'+ cursor.value.room_seq + '. </small>' + cursor.value.room_name + '</li> ');
}
cursor.continue();
} else {
console.log('Entries all displayed.');
if(!($.jStorage.get('reverseroute', ''))) {
reverseroute = 'asc';
} else {
reverseroute = $.jStorage.get('reverseroute', '');
}
appendHref(reverseroute);
asyncCallToPouchDb();
}
};
// Close the db when the transaction is done
transaction.oncomplete = function() {
db.close();
};
};
}
function asyncCallToPouchDb() {
$('#mylist li').each(function(){
var audited = "";
var room_id = $(this).attr('id');
var thisLi = $(this);
audited = callPouchDb(room_id, thisLi);
});
}
function callPouchDb(room_id, thisLi) {
options = {},
pdb = new PouchDB('pouchroomusage');
options.include_docs = true;
var audited = "";
//return new Promise(resolve => {
pdb.allDocs(options, function (error, response) {
result = response.rows;
for (i = 0; i < result.length; i++) {
if (checkIfAudited(result[i], room_id)) {
audited = ' <span class="checked"><i class="fa fa-check"></i></span>';
}
thisLi.append(audited);
}
//thisLi.append(audited);
/*end pdb.allDocs*/
}).then(function (result) {
// handle result
console.log(result);
}).catch(function (err) {
console.log(err);
});
// });
}
function checkIfAudited(row, room_id) {
var today = new Date();
if(is_BST(today) == true) {
var currentHour = today.getHours()+1;
} else {
var currentHour = today.getHours();
}
var currentDay = today.getDate();
var currentMonth = today.getMonth();
var currentYear = today.getYear();
var isAudited = false; ///// <---- define isAudited outside of db iteration scope
var auditTime = new Date(row.doc.timestamp);
var auditHour = auditTime.getUTCHours();
var auditDay = auditTime.getDate();
var auditMonth = auditTime.getMonth();
var auditYear = auditTime.getYear();
if(row.doc.room_id == room_id && currentHour == auditHour && currentDay == auditDay && currentMonth == auditMonth && currentYear == auditYear) {
isAudited = true;
// debug
// console.log('RoomSeq: ' + roomseq + '; auditHour: ' + auditHour + '; currentHour: ' + currentHour + '; auditDay: ' + auditDay);
// console.log('currentDay: ' + currentDay + '; auditMonth: ' + auditMonth + '; currentMonth: ' + currentMonth + '; isAudited: ' + isAudited);
} else {
console.log('No matches');
};
return isAudited
}

Extracting image from API

Trying to output an image from an API but I keep getting an error "Empty JSON string"
function getIcon2(id)
{
var api = "http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=";
var data2 = JSON.parse(UrlFetchApp.fetch(api + id));
return data2.item.icon_large;
}
function iconTest(){
var icon = getIcon2(itemsheet.getRange("C2").getValue());
itemsheet.getRange("D18").setValue(data2);
}
I figured it out.
function getIcon() {
for(var i = 2; i < 500; i++) {
id = itemsheet.getRange("C" + i).getValue()
if(id == "")
return; //If the cell is empty, ignore it.
try {
target = itemsheet.getRange("B" + i);
var api = "http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=";
var raw = UrlFetchApp.fetch(api + id);
var data = JSON.parse(raw);
formula = "=image(\"" + data.item.icon_large + "\",1)";
target.setFormula(formula);
} catch(err) {
Logger.log("getIcon...." + err)
return;
}
}
}

Categories