title: NodeJS pass Array from MongoDB to EJS file
body: <p>i'm trying to make an option tag for every item which is in my Array in MongoDB. I have tried some ways but EJS throws me everytime the same error "gate is not defined"..</p>
  
  <p>Here is my function in my route:</p>
  
  <pre><code>router.post('/neueBuchung_spediteur', (req, res) =&gt; { 
  
          User.findOne({username: req.user}, function (err, user) {
  
              res.render('neueBuchung_spediteur', {
  
                     gate: user.gate
                  });
  });
  </code></pre>
  
  <p>And this is my EJS function:</p>
  
  <pre><code>  &lt;select id="torauswahl" name="torauswahl" style="padding:10px;font-size: large; width: 300px"&gt;
         &lt;% for (var i = 0; i &lt; gate.length; i++){%&gt;
         &lt;option value="&lt;%=gate[i]%&gt;"&gt;&lt;%=gate[i]%&gt;&lt;/option&gt;
         &lt;%}%&gt;
      &lt;/select&gt;
  </code></pre>
  
  <p>thats my full JS file for understanding the logic:</p>
  
  <pre><code>const express = require('express');
  const router = express.Router();
  // Load Buchung model
  const Buchung = require('../DB/models/Buchung');
  const User = require('../DB/models/User');
  const Tor = require(('../DB/models/Tor'));
  const { ensureAuthenticated } = require('../DB/config/auth');
  const passport = require('passport');
  
  //Startseite Breuninger
  router.get ('/startseite_breuninger', ensureAuthenticated, (req, res) =&gt; {
  
      Buchung.find(function (err, buchungen) {
          if (err)
              return res.send(err);
  
          res.render('startseite_breuninger',{
              vorname: req.user.vorname,
              buchungen: buchungen || []
          });
      });
  });
  
  //startseite Spedi
  router.get ('/startseite_spediteur', ensureAuthenticated, (req, res) =&gt; {
  
      Buchung.find(function (err, buchungen) {
          if (err)
              return res.send(err);
  
          res.render('startseite_spediteur',{
              buchungen: buchungen || []
          });
      });
  });
  
  //Buhchungsübersicht mitarbeiter
  router.get('/buchungsuebersicht', (req, res) =&gt; res.render('buchungsuebersicht'));
  //Buhchungsübersicht spedi
  router.get('/neueBuchung_spediteur', (req, res) =&gt; res.render('neueBuchung_spediteur'));
  
  //torauswahl spedi
  router.get ('/torauswahl', (req, res) =&gt; {
  
      Buchung.find(function (err, buchungen) {
          if (err)
              return res.send(err);
  
          res.render('torauswahl',{
              buchungen: buchungen || []
          });
      });
  });
  
  //torverwaltung mitarbeiter
  router.get ('/torverwaltung', (req, res) =&gt;{
  
      Tor.find(function (err, tor) {
          if (err)
              return res.send(err);
          res.render('torverwaltung',{
              tor: tor || [],
          });
      });
  });
  //Update Benutzerdaten Breuni
  router.post('/update_detailansicht_breuninger',(req,res) =&gt;{
      const username = req.body.username;
      const telefon = req.body.telefon;
      const email = req.body.email;
      User.update({username: username}, telefon);
      res.render('detailansicht_breuninger');
  
  });
  
  //insert
  //insert
  
  router.post('/neueBuchung_spediteur',ensureAuthenticated,(req, res) =&gt; {
      const {sendungsstruktur, datepicker, timepicker1, timepicker2, sendungen, EUP, EWP, pakete, bemerkung, teile } = req.body;
      var user = req.user;
      if (errors.length &gt; 0) {
          User.findOne({ username: req.user}, function (err, user) {
              console.log(JSON.stringify(req.user));
              if (err) { throw err; }
              if (user) {
                  res.render('neueBuchung_spediteur', {
                      gate: user.gate || []
                  });
              }
          });
      }
          const newBuchung = new Buchung({
              sendungsstruktur,
              datepicker,
              timepicker1,
              timepicker2,
              sendungen,
              EUP,
              EWP,
              pakete,
              bemerkung,
              teile
          });
          newBuchung.save()
              .then(buchung =&gt;{
                  res.send('saved')
              })
              .catch(err=&gt;console.log(err));
          console.log(newBuchung)
  
  });
  router.post(
      '/login',
      passport.authenticate('local', {
          failureRedirect: '/login'
      }), (req, res) =&gt; {
          if (req.user.admin == "spediteur") {
              res.redirect('/buchungen/startseite_spediteur');
          } else {
              res.redirect('/buchungen/startseite_breuninger');
          }
      });
  
  
  
  module.exports = router;
  </code></pre>
  
  <p>Im thankful for any help :)</p>