Check if a file exists

Hi guys I have folowing code that loads img files into my table that is created with ejs:

<table id="Table1">   
<tr>
    <th>Kickoff</th>
	<th>Status</th>
	<th>Country</th>
	<th>League</th>
    <th>HomeTeam</th>
	<th>AwayTeam</th>
</tr>
<% for (var i = 0; i < result.length; i++) { %>
    <% var fileName = result[i].country + ".png" %> 	
    
	<tr>
	    <td><%= result[i].Kickoff %> </td>
		<td><%= result[i].statustype %> </td>
		<td><img src="images/flags/<%= fileName %>" alt="images/flags/NoFlag.png" style="width:25px;height:15px;"/> <%= result[i].country %></td>
		<td><%= result[i].league %> </td>
        <td><%= result[i].hteam %> </td>
    	<td><%= result[i].ateam %> </td>
    </tr>

<% }; %>
</table>

This works fine , However im having trouble to get a default img to load when no img file is found. Using alt img did not work :confused:

As a second attempt I have also tried to check in the server.js file like so: (but here the code is not working)

  var fs = require('fs');
  var arrayLength = arr.length;
  for (var i = 0; i < arrayLength; i++) {
	var imgfile = arr[i].country
    if (fs.exists('./public/images/flags/' + imgfile + ".png") == true) {      
      //console.log('/public/images/flags/' + imgfile + ".png"); 
      console.log('fs exists');		  
	} else {
	  console.log('Not Found!');   
	}		       
  }	  

I was hoping some brilliant mind would please be so kind to point me in the right direction , or help me write this error handler.

frederik

Because that’s the alternate text.

hmmm ok , any suggestion to how to load an default alternate img when no img is found ?

If you run EJS on the server, I’d simply validate the file names before putting them into the template. If it’s in the browser, check the emitted error event.

thanks for your reply.
maybe it looks like I know what I’m doing with all this fancy code , but I’m a novice at best. I really don’t know what i’m doing here. What you are suggesting is actually what I was trying to do with this part :

  //check for icon
  var fs = require('fs');
  var arrayLength = arr.length;
  for (var i = 0; i < arrayLength; i++) {
	var imgfile = arr[i].country
    if (fs.exists('./public/images/flags/' + imgfile + ".png") == true) {         
      //console.log('/public/images/flags/' + imgfile + ".png"); 
      console.log('fs exists');		  
	} else {
	  console.log('Not Found!');   
	}		       
  }	  

When I run this code in console It just returns “Not Found” for every single row, I think I have messed up with the directory, but the file should be located in var/myproject/public/images/flags

You need to debug … are all variables really what you expect them to be?

second, fs.exists() is a) deprecated and b) asyncronous

Yes I have been debugging like so to:

//console.log(‘/public/images/flags/’ + imgfile + “.png”);

which returns as an example /public/images/flags/England.png which is in that folder

is your module in the correct directory for that to work?

no the module is placed in scrpts/scraper.js

Maybe that is the reason its not working ?

One of them. There is a reason that it’s recommended to only use absolute paths when testing the file system.

my project is structured like so

Main
    package.jason
    package-lock.json
    index.js
    views
        partials
            head.ejs
            header.ejs
            footer.ejs
        pages
            index.ejs
    scripts
        scraper.js
    node_modues

With that you should be able to figure out what the correct relative path should be.

How could I set this up with absolute path then ?

you start with https://nodejs.org/dist/latest-v9.x/docs/api/modules.html#modules_dirname

Thanks for your help and letting me think for myself rather then just provide an quick answer. However this is actually really hard when one does not know any better

based on your suggestions I thought for sure this would work , but still no luck

 if (fs.exists('../public/images/flags/' + imgfile + ".png") == true) {

yes, I have mentioned in post #6 why this can’t work.

phew such fancy words, still quite lost.

  1. Deprecated means it should still work , but is nor recommended method ?

  2. Asynchronous , hmm ?

I guess I really have a lot to learn to be able to continue with my project,

  1. yes

  2. you’re working with the embodiment of asynchronous coding (Node.js) without knowing what ‘asynchronous’ means?

yup that’s right ., pretty crazy . taking some shortcuts to learning , (its not paying of)

how bout this:

if (fs.exists(path.join(__dirname, '../public/images/flags/' + imgfile + ".png")) == true) {

or

if (fs.existsSync('/var/scraper/public/images/flags/' + imgfile + ".png") == true) {