Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

99
Views
EJS template not rendering any data

I am having a problem with ejs templates. I've already worked with it in another project and it worked but in this one it doesn't seem to work all it shows is a blank page and it does not render the cards with the passed data. Here is my "view products" route code and my html code for "viewproducts.ejs".

view products route:

app.get('/viewproducts', function (req, res) {
  var names = [];
  var prices = [];
  var descriptions = [];
  var product = [];
  var length;
  Product.find(function (err, products) {
    if (err) {
      console.log(err);
    } else {
      length = products.length;
      names = products.map(function (i) {
        return i.name;
      });
      prices = products.map(function (i) {
        return i.price;
      });
      descriptions = products.map(function (i) {
        return i.description;
      });
    }
    for (var i = 0; i < length; i++) {
      product.push({
        name: names[i],
        description: descriptions[i],
        price: prices[i],
      });
    }
    console.log(product);
  });
  res.render('viewproducts', { artisanproduct: product });
});

viewproducts.ejs

<body>
    <% artisanproduct.forEach((product)=>{ %>
    <div>
  <div class="card-body">
    <div class="row">
      <div class="col-lg-3">
        <img class="w-100"src="" alt="">
      </div>
      <div class="col-lg-9">
        <h5 class="card-title"><%=product.name%></h5>
        <p class="card-text"><%=product.description%></p>
      </div>
    </div>
    <h2><%=product.price%><span>MAD</span></h2>
    <button type="submit">BUY</button>
    </form>
  </div>
</div>
</div>
<%})%>
  </body>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are calling res.render() BEFORE you've collected any data for it. Product.find() is non-blocking and asynchronous. So, to use its result, your code has to be inside its callback or in a function you call from that callback. Change and simplify to this:

app.get('/viewproducts', function (req, res) {
  Product.find(function (err, products) {
    if (err) {
      console.log(err);
      res.sendStatus(500);    // send error response
      return;
    }
    const product = products.map(item => {
        return {
          name: item.name,
          price: item.price,
          description: item.description
        };
    });
    res.render('viewproducts', { artisanproduct: product });
  });
});

Summary of changes:

  1. Move res.render() inside the Product.find() callback so it has access to that data.
  2. Build product array in one .map() instead of many.
  3. Send error response when there's an error.
  4. Use const or let instead of var.
  5. Remove unnecessary temporary arrays and temporary variables.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!