Display a text file content into electron


#1

Hello

I am new to electron. I have been trying to learn nodejs and have had some progress. I can open a text file stored in my project folder then get its content to print out to console.log();

I don’t know how to get it to print onto my electron index.html file. I am using a windows 10 machine. So I run the electron app by npm start index.html to start the app.

I tried module.exports = and it did not help the only thing it does is print the code out to read the text file.

Please provide some learning advice and instruction on solving this problem.


#2

Mind sharing your code with us? It would be easier to help you if we can see what you’ve gotten done so far.


#3

I would like to put all data in a drop down listbox. Populating the box will depend on this code. Right now I just want it show up correctly on the app

here is the code for the text file

var StrArry = [];

function myApp(){

var lineReader = require(‘readline’).createInterface({
input: require(‘fs’).createReadStream(‘text.txt’)
});

lineReader.on(‘line’, function (line) {
StrArry.push(line);
});

lineReader.on(‘close’, function (line) {

var Ncount = 0;
for(var x =0; x < StrArry.length; x++){
if (StrArry[x] == ‘cat’){

  do { 
  Ncount +=1;    
  x +=1;
   if (StrArry[x] == "") {
       Ncount = 700;
    }
   
   console.log(StrArry[x]);

} while (Ncount<700 );

}

}

});

}

module.exports = myApp;

Here is the .html file

   Hello World!<br>
   <script>
  
   var myApp = require('MyApp.js')

;

    myApp();
   
  document.write(myApp);
   </script>
 
</body>

#4

MyApp should read myApp


#5

Because you just posted the code into the forum (instead of using somewhere like GitHub) and didn’t put it all in code tags, it’s impossible for someone to replicate your exact circumstances. We don’t have your project.json still and we have to guess at the indentation you used. That said, I don’t think that’s your problem.

In JavaScript, functions can be passed around in the same way that variables and objects can. I believe that what you’re doing right here is simply passing the function itself to document.write(). What you should be doing is assigning the results of the function to a variable (output = myApp()) and then passing that variable into document.write().

That said, I don’t see any output functionality in myApp() except console.log(), so as written you should see precisely nothing if it works correctly.


#6

sorry…please explain how to I am new to this site


#7

I will try the output example also.


#8

Discuss uses Github-Flavored Markdown for tags. You can also use the buttons above the editor to format text. But by far the best means to share code is to upload your entire source to somewhere that stores individual files. If you use a GitHub repo, then someone attempting to help you can easily clone your code, fix the problem, and then submit a pull request with the exact changes necessary.


#9

Hello,

I did as you suggested and went on github. if you don’t mind could you please have a look at my code.

Thanks


#10

That code will simply print the code of the function, because you are writing the function to the document when you pass the function to document.write. Plus, your array is a local variable to the function, the script in index.html can’t read it (the way it’s currently written). What you can do is provide a callback that is called when your done with your array or your number or whatever (not sure what it is that you want to write to the document).

In your app.js:

function AniList(callback) {
// Your other code here
// Then, after you're done with the loop:
callback(Ncount); // or AniArray, I'm not sure what you want to write to the document
}

And in your index.html script:

var AniList = require('./app.js');

AniList(function(Ncount) { // or whatever variable you want to write
  document.write(Ncount);
});

#11

Thanks for the response

The code did work but I would like to put an use another array so the console.log results can be placed in there. I am trying this now.

my goal is to get the code to go through the text file and select the line that matches the value of the if statement and print all lines following until it reaches a blank line then stop printing.


#12

hello facekapow

your code does work. Now I am having a little problem when adding another function to the app.js file. I cannot access the callback array. I am still learning.

The goal is still the same that is select one element in a selection input box and then find its contents in an array that will be added to another selection list. example

box 1 (code intiates and populate the selection option by printing the array elements. The array holds a diffrent kinds of animals fish, dog and cat) user selects fish (click-on)
box 2 (code is ran to find fish in another array and dynamically adds selection options to all its elements) the input list shows bass, trout, goldfish, perch etc.

the user then can select which fish he/she wants to select

this the bases of the code but it is not animals.

here is the code and would like to now how should this be modified for the callback to work.
(app.js)

    var zipArry = []; 
    var zipResult= [];
    var aniArry = [];
    var aniResult=[];
   
    var blah = {
    ziplist: function zipList(callback) {
           
    var lineReader = require('readline').createInterface({
    input: require('fs').createReadStream('./HarCntyZip.txt')
    });
    
    lineReader.on('line', function (line) {
        zipArry.push(line);
    });

    lineReader.on('close', function (line) {
    
    var Ncount = 0;
    for(var x =0; x < zipArry.length; x++){

        zipResult.push(zipArry[x]);
                    
    }
  });
    callback(zipResult);
},
 

 anilist: function aniList(callback) {
    

    var lineReader = require('readline').createInterface({
    input: require('fs').createReadStream('./AnimalList.txt')
    });
    
    lineReader.on('line', function (line) {
       aniArry.push(line);
    });

    lineReader.on('close', function (line) {
        
    var Ncount = 0;
    for(var x =0; x < aniArry.length; x++){
        
         aniResult.push(aniArry[x]);
        }
        
   });
     callback(aniResult);
  }
  
 }; 
}
   module.exports = blah;

Here is the code in the index.html file

var blah = require('./apps2.js');
blah.ziplist();
     blah.ziplist(function(zipResult) {
     var select = document.getElementById("zip");  
     for (var i= 0; i<zipResult.length; i++){
                             
     var el = document.createElement('option');
     var opt = zipResult[i];
     el.textContent = opt;
     el.value = opt;
     select.appendChild(el);
    }
   });                    
   
    Blah.anilist (function(aniResult) {
       
       var select = document.getElementById("zipnext");  
       for (var i= 0; i<aniResult.length; i++){
                             
       var el = document.createElement('option');
       var opt = aniResult[i];
       el.textContent = opt;
       el.value = opt;
       select.appendChild(el);

      }

                 
   });