When only showing the title to the client, it results in an undefined value

I have created a schema in mongoosejs that looks like this:

var uploadSchema = mongoose.Schema({
    title            : String,
    happy            : String,
});

I am trying to show the data from my database on the client side (using ejs for templating):

app.get('/yay', function (req, res, next){
        Upload.find({}, function (err, course){
            res.render('./pages/yay.ejs', {course: course});
        });
    });

When displaying it on the client side:

<div class="well">
    <p><%= course %></p>
</div> 

Unfortunately, this code displays the entire database entry including the id and whatWillLearn. I only want to display the title. How can I achieve this? I attempted the following:

app.get('/yay', function (req, res, next){
            Upload.find({}, function (err, course){
                res.render('./pages/yay.ejs', {course: course.title});
            });
        });

However, this just shows 'undefined' on the client side. Any suggestions on how to fix this issue?

Answer №1

It seems like there is an issue with setting the correct variable in the object passed to render when returning an array.

Try this approach (to retrieve the first course in the array):

app.get('/yay', function (req, res, next){
  Upload.find({}, function (err, courses) {
    res.render('./pages/yay.ejs', { happy: courses[0].title });
  });
});

If there are no courses, this method will not work as intended. You may consider iterating through all returned courses and printing them out, or modify Upload.find to Upload.findOne to retrieve only one item from the database.

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

extracting values from deeply nested JSON array in JavaScript

Is there a way to extract values from a deeply nested JSON array? I'm looking to retrieve all pairs of (nameValue and value) from the JSON provided below var json = [{ name: 'Firstgroup', elements: [{ ...

Generate an HTML dropdown menu based on the item selected from the autocomplete input field

I have a PHP page that searches the database and returns JSON results to an autocomplete input field: When I display the response from the PHP file (as shown above), it looks like this: { "success": true, "results": [{ "name": "Bananas, r ...

Updating a nested object within an array in a ReactJs component

I am struggling with updating a nested object within an array in the state of my React application. My goal is to determine if an item already exists in the state based on its name. Here's what I'm aiming for: Add an item to the cart. If th ...

Selecting a color in Vuetify's color picker triggers the @

I have incorporated the Vuetify color picker into my project for changing the background and text colors of elements. Currently, I am storing the hex color values in Firestore by using the @input="updateColor" attribute on the v-color-picker to t ...

The best practices for securing routes using react-router and passport on the backend

I have implemented React and Node.js with passport.js on the backend to handle the authentication for my app. My frontend React code makes a call to the backend API to fetch the authorized user using action reducers. Everything seems to be working fine, bu ...

Implementing Autocomplete feature in Reactjs with Ant Design Library

In my React application, I created an autocomplete feature with a list of options for the user to select from. Example in Action: Click here to see the demo List of available options: const options = [ { label: "hello", value: "1" ...

Locate the child element that has a particular class assigned to it

I need help writing a code to search through all children elements in order to find a div with a specific class. Unfortunately, the DIV I am looking for does not have an ID. Below is the sample HTML that I will be working with: <div class="outerBUB ...

Transforming the response.render method in Express to be a promise-enabled

I have a task at hand where I need to develop a render method that constructs HTML blocks into a string. Initially, it appears to be working fine, but I ended up using the infamous "new Promise" and now I'm not sure if my approach is correct. Here&apo ...

Unable to fetch the identification number from the database

I'm encountering an issue with retrieving the ID from my database: Here is a snapshot of my database: Below is my event controller class: <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Event ...

Using pdfkit to create a PDF and then returning it as a base64 string from a function

I am attempting to utilize PDFKit to produce a PDF file and then retrieve it as a base64 string. Here is the code snippet I am using: function generatePDFDocument(data){ let doc = new PDFDocument(); var bufferChunks = []; doc.on('readabl ...

Creating a personalized image download feature in PhotoSwipe.js

I am currently working on a project that involves using the PhotoSwipe gallery. At line 175, there is code url:items[0].hqImage. I want to use the index of the current image instead of 0. How can I achieve this? I attempted to utilize the pswp.listen(&ap ...

What do I need to add in order to connect a controller to a form submission?

Let's set the scene: There are multiple newsletter forms scattered across a webpage, all needing to perform the same action. This action involves making an AJAX request with certain data and displaying a message using an alert once the request is comp ...

The drawing library (Google Maps) failed to load

I am looking to integrate drawing mode into Google Maps for my project. Below is the code snippet from my View: <!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <me ...

Explore bar with search button

I'm facing an issue with the code I have for searching on my website. Currently, when a user fills in their search word and presses enter, it executes the command. However, I would like to only run the search script when the user clicks a button inste ...

Vue event manager, accessible for all components

I have created a new Vue instance and assigned it to the window object, thinking that it would be accessible throughout all components. I expected this setup to allow me access to events emitted anywhere within my application. However, it seems like this ...

When clicking on the register button in Node.js, no activity is initiated

Once upon a time, there was a humble express.js app that needed to establish a connection with a mysql database and retrieve user information for registration within the said database. Despite successfully connecting to the database, nothing would happen w ...

Why does the MEAN Stack continue to route using the '#' symbol in the URL?

Currently navigating the realm of back end development. Started off by following a guide on express from thinkster. In need of some clarification. Initially, I grasped that front-end and back-end routing serve different purposes. Front-end routing relates ...

Passport.js: Navigating Users in the Right

I've been working on integrating passport.js, passport-google-oauth, and nodjes to retrieve a user's profile locally. However, I'm facing an issue with the redirect after logging in. Although the information is successfully loaded (I can acc ...

Establish a connection to couchDB using JavaScript on the server side

I'm working on a piece of code that involves using Restify to set up a server in node.js and create specific routes. My goal is to interact with CouchDB by performing actions such as GET, POST, DELETE, and PUT. var restify = require("restify"); var s ...

Unable to retrieve the value property from document.getElementById as it is null

Can someone help me with reading the input field value in my code? <input type="text" name="acadp_fields[1200]" class="text" placeholder="" value="December 26, 1969"> Here is the code snippet I am us ...