'Javascript can't write to a file
I want the code to write datalogs to JStest.csv file after 5 clicks done by the user are completed, and I have created a CSV file call JStest in the Downloads folder. But when I run my code, it says it can't find this file. Please help ! This is the error message I got: Error: ENOENT: no such file or directory, open '/Downloads/JStest.csv' Here's my code:
const app = require('electron').remote.app;
const fileDir = app.getPath('desktop');
const path = require("path");
var fs = require('fs');
// this will hold all the data we need
var dataLog = "";
// this will count how many clicks have occured
var clicks = 0;
var maxTrials = 5;
var iconArray = document.getElementById("iconDrawer");
// reference our start button
var startButton = document.getElementById("startBtn");
// display how many tasks a user has completed
var counterDisplay = document.getElementById("counter");
// display the target icon to click (find the element with this HTML tag)
var indicator = document.getElementById("indicator");
// element that holds all your icons
var parent = document.getElementById("iconDrawer");
// array of all icons (hint: use the parent var to reference its children icons)
var icons = parent.children;
var i0 = icons[0];
var i1 = icons[1];
var i2 = icons[2];
var i3 = icons[3];
var i4 = icons[4];
/////////////////////////////////////////////////////////////////////////////////////
// TODO: Set the filepath, so you know where the .csv file is going!
/////////////////////////////////////////////////////////////////////////////////////
function save()
{
// change the filepath in the writeFile() function
fs.writeFile( path.resolve(fileDir, "/Users/lidaochang/Desktop/Downloads/JStest.csv"), dataLog, (err) => {
if (err) alert(err);
alert("all tasks are done");
});
}
function randomIcon()
{
// Generate a random number in the appropriate range
var iconIndex = Math.random()*4;
iconIndex = Math.round(iconIndex);
return iconIndex;
}
var timedClick = function()
{
// disables the start button, so user can't press it twice
//startButton.onclick = function(){};
// call randomIcon function to get random index and the matching icon
var targetIndex = randomIcon();
var targetIcon = icons[targetIndex];
indicator.src = targetIcon.src;
// start timing right here
var startTime = performance.now();
// this is where we are going to start watching for clicks on icons
// this loop will add an onclick function to each icon
for(var i=0; i < icons.length; i++)
{
icons[i].onclick = function()
{
// everything in here will occur when an icon is pressed
// stop timing and record how long it took
// calculate time elapsed
// record whole milliseconds
var endTime = performance.now();
var timeTaken = endTime - startTime;
timeTaken = Math.round(timeTaken);
alert("that took " + timeTaken);
// only 1 icon can be clicked at a time, so disable all the onclicks now!
// loop through all the icons and disable the function like we did with the start button
for(var j=0; j < icons.length; j++) {
if (j != targetIcon)
icons[j].onclick = function(){};
}
// record the time and positions of the target icon and the icon the user actually pressed
// this is to be stored in a new line in the 'dataLog' variable
// append to the 'dataLog' variable
var iconClicked = this.id[1]; // INCLUDE THIS
var data = "";
data.concat(i, " ", targetIndex, " ", iconClicked, " ", timeTaken);
// add to the end of the 'dataLog' variable as explained above
dataLog += data;
// increment clicks completed
clicks += 1;
// update what the counterDisplay says!
// modify the innerHTML property of counterDisplay
// it shows the user how many clicks have currently been completed
counterDisplay.innerHTML = clicks + " tasks out of 5 completied";
// if maxTrials is reached, then data collection is over, so call save and reset 'clicks' and 'dataLog'
if (clicks == maxTrials) {
save();
clicks = 0;
dataLog = "";
}
to starting the trial
startButton.onclick = timedClick;
}
}
}
window.onload = function()
{
startButton.onclick = timedClick;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
