1

I am just starting with javascript / express / mongodb. As a little starting project I want to do a simple task manager. The todos should be divided by prios. So every entry has it's flag "high", "mid" or "low".

However, I can't manage to show the different entries on the index page.

The error message is: "tasks_low is not defined"

This is the relevant js snippet:

app.get('/', function(req, res){
    db.tasks.find({prio: 'high'}, function (err, highs) {
        res.render('index', {
            title: 'Aufgaben High:',
            tasks: highs
        });
    })

});

app.get('/', function(req, res){
        db.tasks.find({prio: 'low'}, function (err, lows) {
        res.render('index', {
            title: 'Aufgaben Low:',
            tasks_low: lows
        });
    })

});

This is my index.ejs file:

<h1>Prio High</h1>
<ul>
    <% tasks.forEach(function(tasks){ %>
    <li><%= tasks.task %> <%= tasks.prio %> - <a class="deleteUser" data-id="<%= tasks._id %>" href="#">x</a></li>
<% }) %>
</ul>
<br><br>

<h1>Prio Low</h1>
<ul>
    <% tasks_low.forEach(function(tasks_low){ %>
    <li><%= tasks_low.task %> <%= tasks_low.prio %> - <a class="deleteUser" data-id="<%= tasks_low._id %>" href="#">x</a></li>
<% }) %>
</ul>

However, when I compile to different views it all works well!

app.get('/tasks_high/', function(req, res){
    db.tasks.find({prio: 'high'}, function (err, highs) {
        res.render('index', {
            title: 'Aufgaben High:',
            tasks: highs
        });
    })

});

app.get('/tasks_low/', function(req, res){
        db.tasks.find({prio: 'low'}, function (err, lows) {
        res.render('index', {
            title: 'Aufgaben Low:',
            tasks_low: lows
        });
    })

});
daniel_e
  • 257
  • 1
  • 11

1 Answers1

1

Just create a collection entry with title + task:

// Task collection structure
var Task= mongoose.model('Tasks', {
    title: String,
    priority: String,
});

And then filter each task based in its priority as you're doing, but using the same collection

Ignacio Ara
  • 2,476
  • 2
  • 26
  • 37
  • I thought I am working in the same collection. It's all in collection "tasks". The entries have the attributes "id", "task" and "prio". I filter by db.collection.find – daniel_e May 08 '18 at 14:33
  • Ok in that case add that part of the code. Now my question is why you're `res.render` with an attribute that doesn't exists (tasks_low). You should return the `prio` – Ignacio Ara May 08 '18 at 14:51
  • I thought I define it with `tasks_low: lows`. That may be the problem. Where exactly does it have to be defined? – daniel_e May 08 '18 at 17:21
  • You should follow the structure that I posted, as `name: type` when you define the collection – Ignacio Ara May 09 '18 at 07:08