简体   繁体   中英

Compile two different mongodb entries

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
        });
    })

});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM