title: ReferenceError: 'pictures' is not defined. Using Node JS
body: <p>Searched all over the place but can't get it running. I'm new here, so kinda hard for me also to search or fix the right thing.. Trying to fetch data from the database, but the pictures variable is not defined it says, I got it running before, maybe just a type error of something like that, but I tried already lots of things and i'm running out of time for my school project. This is my code:</p>
  
  <p>index.ejs:</p>
  
  <pre><code>&lt;main&gt;
      &lt;div class="jumbotron"&gt;
          &lt;h1&gt;Formula 1, see it, feel it!&lt;/h1&gt;&lt;/br&gt;
          &lt;p&gt;Welkom! Op deze site vind u de meest mooie foto's van de Formule 1.&lt;/p&gt;
      &lt;/div&gt;
  &lt;/main&gt;
  
  &lt;div class="jumbotron"&gt;
  
         &lt;% if(pictures.length){ %&gt;     
  &lt;table&gt;
      &lt;tr&gt;
          &lt;th&gt;Titel&lt;/th&gt;
          &lt;th&gt;Locatie&lt;/th&gt;
          &lt;th&gt;Datum&lt;/th&gt;
          &lt;th&gt;Naam&lt;/th&gt;
          &lt;th&gt;Waardering&lt;/th&gt;
  
      &lt;/tr&gt;
          &lt;% } %&gt;
          &lt;% for (var i = 0; i &lt; pictures.length; i++) { %&gt;
      &lt;tr&gt;
  
          &lt;td&gt;&lt;%= pictures[i].titel %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= pictures[i].locatie %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= pictures[i].datum %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= pictures[i].naam %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= pictures[i].waardering %&gt;&lt;/td&gt;
          &lt;% } %&gt;
      &lt;/tr&gt;                   
  &lt;/table&gt;    
  
  &lt;/div&gt;
  
  &lt;footer&gt;
      &lt;% include ../partials/footer %&gt;
  &lt;/footer&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>index.js</p>
  
  <pre><code>    var express = require('express');
  var router = express.Router();
  
  router.get('/', function(req, res) {
      req.getConnection(function(err, connection) {
          connection.query('SELECT * FROM pictures', function(err, results) {
              res.locals.pictures = results;
              console.log(results);
              res.render('../views/pages/index');
          });
      });
  });
  
  module.exports = router;
  </code></pre>
  
  <p>app.js: </p>
  
  <pre><code>var express = require('express'),
      path = require('path'),
      favicon = require('serve-favicon'),
      logger = require('morgan'),
      cookieParser = require('cookie-parser'),
      flash = require('connect-flash'),
      expressValidator = require('express-validator'),
      session = require('express-session'),
      passport = require('passport'),
      LocalStrategy = require('passport-local').Strategy,
      bodyParser = require('body-parser'),
      multer = require('multer'),
      mongo = require('mongodb'),
      mongoose = require('mongoose'),
      mysql = require('mysql'),
      myConnection = require('express-myconnection');
  
  var db = mongoose.connection;
  
  var dbOptions = {
      host: 'localhost',
      user: 'fcdeen_user',
      password: 'wEvGT9ctm8KUxdTD',
      database: 'fcdeen'
  };
  
  var indexRoutes = require('./routes/index'),    
      loginRoutes = require('./routes/login'),
      uploadRoutes = require('./routes/upload');
  
  var app = express();
  
  //Use session
  app.use(session({
    secret: "Its_A_veRy_DarK_SecreT",
    resave: false,
    saveUninitialized: true
  }));
  
  // Voeg de bodyparser middleware aan de app toe (POST requests)
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({extended: true}));
  
  // set the view engine to ejs
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'ejs');
  
  // Voeg connectie middleware toe aan app
  app.use(myConnection(mysql, dbOptions, 'single'));
  
  // index page 
  app.get('/home', function(req, res) {
      res.render('pages/index');
  });
  
  // index page 
  app.get('/index', function(req, res) {
      res.render('pages/index');
  });
  
  // upload page 
  app.get('/upload', function(req, res) {
      res.render('pages/upload');
  });
  
  // login page 
  app.get('/login', function(req, res) {
      res.render('pages/login');
  });
  
  app.listen(8080);
  console.log('Launch at port 8080');
  </code></pre>
  
  <p>And this is the error:</p>
  
  <pre><code>ReferenceError: C:\School\SSS 2015\testen\node-ejs-master\views\pages\index.ejs:21
     19| &lt;div class="jumbotron"&gt;
     20|   
  &gt;&gt; 21|   &lt;% if(pictures.length){ %&gt;     
     22| &lt;table&gt;
     23| &lt;tr&gt;
     24| &lt;th&gt;Titel&lt;/th&gt;
  
  pictures is not defined
     at eval (eval at &lt;anonymous&gt; (C:\School\SSS 2015\testen\node-ejs-master\node_modules\ejs\lib\ejs.js:242:14), &lt;anonymous&gt;:33:292)
     at eval (eval at &lt;anonymous&gt; (C:\School\SSS 2015\testen\node-ejs-master\node_modules\ejs\lib\ejs.js:242:14), &lt;anonymous&gt;:35:68)
     at C:\School\SSS 2015\testen\node-ejs-master\node_modules\ejs\lib\ejs.js:255:15
     at Object.exports.render (C:\School\SSS 2015\testen\node-ejs-master\node_modules\ejs\lib\ejs.js:293:13)
     at View.exports.renderFile [as engine] (C:\School\SSS 2015\testen\node-ejs-master\node_modules\ejs\lib\ejs.js:323:20)
     at View.render (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\view.js:126:8)
     at tryRender (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\application.js:639:10)
     at EventEmitter.render (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\application.js:591:3)
     at ServerResponse.render (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\response.js:961:7)
     at C:\School\SSS 2015\testen\node-ejs-master\app.js:61:6
     at Layer.handle [as handle_request] (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\layer.js:95:5)
     at next (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\route.js:131:13)
     at Route.dispatch (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\route.js:112:3)
     at Layer.handle [as handle_request] (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\layer.js:95:5)
     at C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\index.js:277:22
     at Function.process_params (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\index.js:330:12)
     at next (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\index.js:271:10)
     at C:\School\SSS 2015\testen\node-ejs-master\node_modules\express-myconnection\lib\express-myconnection.js:123:9
     at Layer.handle [as handle_request] (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\layer.js:95:5)
     at trim_prefix (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\index.js:312:13)
     at C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\index.js:280:7
     at Function.process_params (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\index.js:330:12)
  </code></pre>
  
  <p>Thanks for the help!</p>