Merge two separate mongodb records

Just getting started with javascript / express / mongodb. For my initial project, I am working on a simple task manager where todos are categorized by priority - "high," "mid," or "low."

However, I am facing an issue when trying to display different entries on the index page.

The error message states: "tasks_low is not defined"

Here is the relevant javascript snippet:

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

});

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

});

This is my index.ejs file:

<h1>High Priority</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>Low Priority</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>

Interestingly enough, everything works well when I compile it into separate views!

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

});

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

});

Answer №1

To start, simply make a new entry in the collection with both a title and a task:

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

Then, when filtering tasks based on their priority, continue using the same collection for consistency.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The mobile navigation in HTML has a slight issue with the ::after pseudo-element, specifically within the

As I prepare to launch my website, I have made adjustments to the mobile layout by moving the navigation links to a navigation drawer. The template I am using included JavaScript scripts to handle this navigation change. However, I encountered an issue whe ...

Error encountered while using Chart.js with JSON dataset

Struggling to make this work... Here are the necessary scripts: <script src="Chart.js"></script> <script src="jquery-1.11.3.min.js"></script> This is the full code I am working with: <body> <div id="chartCanvas"> &l ...

What could be causing Jquery to not function properly in an Angular controller?

Can someone please help me understand why my code is not working in JSFiddle? Any assistance would be appreciated! UPDATE: I have to admit, the negative feedback is disheartening. I may not know everything, but asking questions is how we learn. What' ...

Accessing form data using Vue on submit

I'm working on a project that involves creating a form with a single input field and saving the data to a database. The technology I am using is Vue.js. Here is the template I have designed: <form class="form-horizontal" @submit.prevent="submitBi ...

The JavaScript file fails to load when accessing port 8080

As I embark on my journey into backend development, please bear with me. Currently, I am working on a JavaScript program that retrieves text data from my localhost. I have set up an HTTP server using Node.js which serves as a regular HTTP server. The serve ...

Deciding whether an altered image has been successfully loaded

Currently, I am stuck on a minor point while creating a small gallery using jQuery. The code snippet looks like this: <script type="text/javascript> $(document).ready(function(){ $('#thumb1').click(function(){ $('#fullimage ...

An easy guide to using validators to update the border color of form control names in Angular

I'm working on a form control and attempting to change the color when the field is invalid. I've experimented with various methods, but haven't had success so far. Here's what I've tried: <input formControlName="pe ...

Node.js: Calculating the number of requests processed per second in the http.get()

In my node.js project, I am creating a simple application for sending HTTP requests. var http = require('http'); var options = { host: 'www.example.com', port: 80, path: '/index.html' }; for(var i = 0; i < 500; i++ ...

What is the best way to transfer a PHP string to JavaScript/JQuery for use in a function?

Within my PHP code, I have the following: $welcome = "Welcome!"; echo '<script type="text/javascript">addName();</script>'; Additionally, in my HTML/script portion: <a id="franBTN"></a> <script type="text/javascript ...

What is the importance of having Express in the frontend?

As someone who is relatively new to the world of JavaScript and web applications, I recently came across an Express web application project that contained a public directory, client directory, and server directory. This has raised some questions for me. I ...

What are the best methods for looping through ids in MongoDB and executing actions on them?

I am working with an entity object that has the following response: [ { "public": false, "_id": "5eb6da3635b1e83", "createdAt": "2020-05-09T16:28:38.493Z", "updatedA ...

Invoke the app.js function within the Node.js route file

Within app.js, there is a function that I am attempting to invoke from a routes file. This is just a simplified example. app.js var express = require('express'); var app = express(); var customFunction = function() { return 'result&apo ...

Tips for saving a document in a table without using the _id field

I want to save the employee object without the _id attribute, just the "employee" attribute as shown below: "employee" :[ { "name" : "leila", "idemployee" : ObjectId("59319505efa50b137477a1f4"), ...

Utilizing MongoDB's filter method to query data with the flexibility of including

How can I create a method in node js using mongodb to filter users based on the following criteria: both batch and course only batch if course is not entered only course if batch is not entered // Filtering students by either course or batch export c ...

Guide to setting up a datePicker in a cellEditorSelector

In my grid, I want to have different editors for different rows within the same column. The ag-Grid documentation provides information on how to achieve this using colDef.cellEditorSelector as a function: link I have successfully created unique editors fo ...

What is the best way to delay an observable from triggering the next event?

In my Angular project, I am implementing RxJs with two subjects. s1.next() s1.subscribe(() => { // perform some operation and then trigger the event for s2 s2.next() }); s2.subscribe(() => { // perform some operat ...

Exploring uncharted territory with the Navigator component in React Native

I am experiencing an issue with undefined navigator when using React Native MessageTabs: _onPressItem = (item) => { const { navigate } = this.props.navigation; //console.log(JSON.stringify(item)); navigate('SingleConversation', {id ...

Automatically collapse the Shadcn-UI Accordion when the mouse exits

For my self-education project, I am working on building a sidebar with an accordion using Shadcn-ui, NextJS (13.4), React, and Node. Being new to these technologies, I have encountered a problem that I can't seem to solve. The sidebar expands on mous ...

A method that sorts an array of objects based on their properties

I am currently working with two specific objects: clinics: [ { id: 1, name: 'New Hampshire Veterinarian Clinic', plans: [ 'handle123', 'handle567', ] }, { ...

Unable to process form submission with AngularJS + Stormpath

I am facing an issue with form submission. Even though I believe that the login and password data are being sent correctly, nothing happens when I submit the form. I am attempting to submit the form without using ngSubmit because it is not feasible in my s ...