for loop continues to run even when "break" is used - javascript

I am trying to run a check within a folder that will display only one alert if there is any other file than extension - '.txt'
Here is my code:
for (let file of files) {
const ext = path.extname(file);
console.log(typeof file)
if (ext != '.txt') {
console.log(ext)
console.log("The error should have popped up")
dialog.showErrorBox('Non TXT file detected', 'there is a non-txt file in the folder')
break
}
else {
console.log("Not Working")
}
}
The issue that I am having is that say there were 10 ".txt" files in the folder, if I renamed one of them to ".test" I would receive 10 alerts. Is there a way to as soon as the "dialog.showErrorBox" has been called prevent to for loop for continuing.
I receive the same amount of errors no matter which file i change the extension for.

I replicated your code, and it seems to work fine: https://repl.it/repls/ExcellentVapidApplicationframework
Maybe an issue caused by ASI, as Andreas suggested?

Related

Why does my typescript program randomly stop running?

I wrote a very simple typescript program, which does the following:
Transform users.csv into an array
For each element/user issue an API call to create that user on a 3rd party platform
Print any errors
The excel file has >160,000 rows and there is no way to create them all in one API call, so I wrote this program to run in the background of my computer for ~>20 hours.
The first time I ran this, the code stopped mid for loop without an exception or anything. So, I deleted the user rows from the csv file that were already uploaded and re-ran the code. Unfortunately, this kept happening.
Interestingly, the code has stopped at non-deterministic iterations, one time it was at i=812, another at i=27650, and so on.
This is the code:
const main = async () => {
const usersFile = await fsPromises.readFile("./users.csv", { encoding: "utf-8" });
const usersArr = makeArray(usersFile);
for (let i = 0; i < usersArr.length; i++) {
const [ userId, email ] = usersArr[i];
console.log(`uploading ${userId}. ${i}/${usersArr.length}`);
try {
await axios.post(/* create user */);
await sleep(150);
} catch (err) {
console.error(`Error uploading ${userId} -`, err.message);
}
}
};
main();
I should mention that exceptions are within the for-loop because many rows will fail to upload with a 400 error code. As such, I've preferred to have the code run non-stop and print any errors onto a file, so that I could later re-run it for the users that failed to upload. Otherwise I would have to check whether it halted because of an error every 10 minutes.
Why does this happen? and What can I do?
I run after compiling as: node build/index.js 2>>errors.txt
EDIT:
There is no code after main() and no code outside the try ... catch block within the loop. errors.txt only contains 400 errors. Even if it contained another run-time exception, it seems to me that this wouldn't/shouldn't halt execution, because it would execute catch and move on to the next iteration.
I think this may have been related to this post. The file I was reading was extremely large as noted, and it was saved into a runtime variable. Undeterministically, the OS could have decided that the memory demanded was too high. This is probably a situation to use a Readable Stream instead of a readFile.

fs.watch calling method when not supposed to?

I'm rather new to node.js and javascript aswell, and I keep running into an error with the code below.
fs.watch('F:/junk', (eventType, filename) => {
if(filename && filename.split('.')[1].match("zip")) {
try {
var zip = new azip(dir + filename);
} catch(err) { return console.log(err); }
try {
zip.extractAllTo(dir + filename.split('.')[0], false);
} catch(err) { return console.log(err); }
}
});
and the error. the error only occurs after successfully running extractAllTo (adm-zip), and doesn't occur at all if I take that bit out.
if(filename && filename.split('.')[1].match("zip")) {
TypeError: Cannot read property 'match' of undefined
I'm using the same file for testing every time, with the name dummyfile.zip
I can create another if statement within the first one and it'll work fine, but seems a bit redundant doing it that way
You must test for the existence of array element 2 (or index 1). There are numerous reasons. Without knowing operating system and file structure it is impossible to tell you. For instance, fs.watch() will return a directly - if the directory has no '.', then fail. This is the same if a file has no extension.
To validate this, in the console do this:
let str = "directory name";
str.split('.');
str.split('.')[1].match('test');
view the output.
Consider
if(filename && filename.includes('.zip')) {
// work
}
}
code is a little more verbose, but will avoid the error caused by directories and files that do not have extensions and files with leading dots, etc.

node readFile wont fire callback

So I am trying read a PDF file and send its buffer as an attachment to an email. The strange thing is I have never had the problem with fs.readFile before, the callback just never fires. I have tried checking the pdf if I can open it, if anything is corrupt but it seems fine.
const destination = './temp/somthing.pdf'
function encodeToBase64(destination, callback) {
return fs.readFile(destination, function (err, data) {
if (err) {
return callback(err);
}
return callback(null, new Buffer(data).toString('base64'));
});
}
I use VSCode and have added breakpoints on all the returns and the node debugger is able to reach the first return which is the readFile, but once I go to the next step my CPU starts to work like a boss and the VSCode node debugger shows messages saying node is unresponsive.
I am at a total loss here as to what is going on. I have tried multiple pdf files aswell but to no avail.
EDIT:
I do not know if this will help but I am on Node v6.9.3

Checking if File Was Downloaded on Windows With Protractor

I am trying to run a test where a file gets downloaded from our web application. The problem that I am running into however is that the file already exists and I have an if statement to handle it and delete it prior to downloading another one. The problem is though that it never goes into the if statement.
I know that my file path is correct. Is there something else I am doing wrong here?
const fs = require('fs')
fit('Download Visible Records', () => {
const filename = `C:\\Users\\me\\Downloads\\DataExport_2017-04-18-10-04am.csv`
if (fs.existsSync(filename)) {
console.log('Are we getting here??') // NEVER ACTUALLY GETS HERE EVEN THOUGH THE FILE DOES EXIST
// Make sure the browser doesn't have to rename the download
fs.unlink(filename)
console.log('how about here?')
}
claims.downloadVizRecords()
browser.driver.wait(() => {
expect(fs.existsSync(filename)).toBe(true)
}, 10000)
})
My code works fine:
if (fs.existsSync('D:/CI/script/result/Report.html')) {
fs.unlinkSync('D:/CI/script/result/Report.html');
}

Debugging a Windows store app - error messages always show the same line number

I've set up error logging on my windows store app, which reports the error message and the line number in the code.
I've been getting two error messages from the same line, line 301. The error messages are
The process cannot access the file because it is being used by another process.
Access is denied.
Based on the First error message I presume the error is with my autosave function, but without the line number I can't say where it's failing. Here's my autosave code
function autosave()
{
if ((localSettings.values["useAutoSave"] == null || localSettings.values["useAutoSave"] == "true")) {
editorContent = editor.textContent;
var text_length = editorContent.length;
if (editedSinceSave && text_length > 0) {
localSettings.values["lastContent"] = text_length;
setStatus("<span class='loader'></span>autosaving", 2000);
writeTempFile();
}
}
window.setTimeout(autosave, _autosave_timeout);
}
function writeTempFile()
{
try{
tempFolder.createFileAsync("tempFile.txt", Windows.Storage.CreationCollisionOption.replaceExisting)
.then(function (theFile) {
return Windows.Storage.FileIO.writeTextAsync(theFile, editor.textContent);
}).done(function () {
localSettings.values["lastPos"] = _lastStartPos;
});
}
catch (e) {
// statements to handle any exceptions
logError(e); // pass exception object to error handler
}
}
Even we I move all my functions around and recompile my code, the error is always at line 301. I suspect that the errorline I'm seeing is actually from whatever underlying js files are used to run my app, but I don't know where to access them. How the hell do I debug this?
Make sure there's not a capability that you have not declared. It doesn't look like your code is using anything special, but it's worth a try.

Categories

Resources