skip to Main Content

Using Node JS, Electron and the FS module I have created a function within a seperate .js file that is called into my HTML as a script and triggered from a button with onclick="ReadSerials()" however it always takes two button presses for the if statement to trigger correctly when the "ReadSerials" function is called. It triggers and does exactly what I’m expecting every time on the second button press.

the "serialnumberinput" element is a text-box where a user would type in the serial number of their product and if the function can find that serial number within the .csv file it returns the row outputting it into HTML on the screen.

function ReadSerials() {
  const fs = require("fs");
  const { parse } = require("csv-parse");

  var serialnumber = document.getElementById("serialnumberinput").value;

  fs.createReadStream("./SER5YEAR.csv")
    .pipe(parse({
      delimiter: ",",
      from_line: 1
    }))
    .on("data", function(row) {
      var inputbg = document.getElementById("serialnumberinput");
      if (row.includes(serialnumber)) {
        console.log(row);
        var logger = document.getElementById('serialselect');
        console.log = function() {
          logger.innerHTML += '<td class="serialresult">' + row[1] + '</td>';
          logger.innerHTML += '<td class="serialresult">' + row[2] + '</td>';
          logger.innerHTML += '<td class="serialresult">' + row[3] + '</td>';
          logger.innerHTML += '<td class="serialresult">' + row[4] + '</td>';
          logger.innerHTML += '<td class="serialresult">' + row[5] + '</td>';
          logger.innerHTML += '<td class="serialresult">' + row[6] + '</td>';
          document.getElementById("serialsubmit").style.visibility = "hidden";
          inputbg.style.backgroundColor = "#63c63c33";
          return;
        }
      } else {

      }
    })
}

I have tried simplifying the function so the if statement just performs the style change as I thought the HTML changes may be triggering too soon after the read stream is created however it did not make any difference. I also tried adding a timeout between the readstream creation and the rest of the function.

If I simplify the entire function to a basic if/else (no files, html elements or data input involved) it does trigger first time so I know it’s an issue somewhere in my function I just can’t seem to figure out which part.

This is my first project in Electron/Node and I’m fairly new to JS in general so apologies if I’m missing something incredibly obvious however I have spent quite a bit of time reading through posts with similar issues but none I can find describe this particular problem.

2

Answers


  1. Chosen as BEST ANSWER

    For anyone who may come across this post with a similar issue - the comment from @Teemu fixed the issue. It was the repeated use of console.log that caused issues. In my case the second use of console.log was unnecessary so removing it fixed my problem.


  2. It’s maybe not the answer to your question, but If you want to have a tabled output in the console, try the console.table() functionality and then add your styling changes directly into your if clause.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search