I'm trying to do "Orders page" that has a list of orders in a single page, but it should have seperate sections. One is for "Already made" and the other is for "Pending". It has a simple database with a field "isDone" that is either true or false. So as I imagine, if it is true, then it should appear on "Already made" section, but the problem is, that if statment doesn't work for some reason in .ejs.

<body class="container">
        <main>
            <div class="jumbotron">
                <h1>Pending</h1>
            </div>
            <% orders_list.forEach(function(order){ %>
                <% if(order.isDone == false) { %>
                    <div>
                    <h3><a href='<%= order.url %>'><%= order.name %></a> - <%= order.price %> euru</h3>
                    <hr>
                    </div>
                <% } %>
            <% }) %>
            <div class="jumbotron">
                <h1>Already made</h1>
            </div>
            <% orders_list.forEach(function(order){ %>
                <% if(order.isDone == true) { %>
                    <div>
                    <h3><a href='<%= order.url %>'><%= order.name %></a> - <%= order.price %> euru</h3>
                    <hr>
                    </div>
                <% } %>
            <% }) %> 
        </main>
</body>
var express = require('express');
var router = express.Router();
var Order = require('../models/order');

router.get('/orders', function(req, res){
    Order.find({})
    .exec(function(err, list_orders){
      if (err) {return next(err)};
      // If Successful
      res.render('../views/pages/index', {orders_list : list_orders});
    });
  });

module.exports = router;
var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var OrderSchema = new Schema(
  {
    isDone: {type: Boolean, required: true},
    name: {type: String, required: true},
    quantity: {type: Number, required: true},
    price: {type: Number, required: true},
    phone: {type: Number},
  });

  OrderSchema
  .virtual('url')
  .get(function(){
      return '/order/' + this._id;
    });

module.exports = mongoose.model ('Order', OrderSchema);

I expect 'isDone' == true orders to go to 'Already made' section, and 'isDone' == false orders should go to the 'Pending' section. But not even single item appears. Database is connected successfully, if there are no if statments then orders appear on the page.

1 Answers

0
shareyar On Best Solutions

In your if condition you have quote your true value because ejs file is taking it as a value instead of a boolean value , so quote your true like this and it will work fine.

 <% if(orders_list[i].isDone == 'true'){ %>
   <div>
    <h3><a href='<%= orders_list[i].url %>'><%= orders_list[i].name %></a> - <%= orders_list[i].price %> euru</h3>
     <hr>
     </div>
  <% } %>