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

Guide on generating a fresh Mongoose record and making modifications before finalizing and storing it

I'm currently in the process of creating a brand new Mongoose document. Here is my approach: let freshTrade = new TradeModel({ userId: userId, symbol: symbol }) My intention is to then transfer this data to another server to obtain additional inf ...

Steps for adjusting the matMenuTriggerFor area so it only triggers when hovering over the arrow

Hello there! I'm currently working on adjusting the trigger area for opening the next menu panel. Right now, the next menu panel opens whenever I hover over either the title or the arrow. However, my goal is to have the menu open only when I hover ove ...

Creating head tags that are less bouncy

After running my code, I noticed that the headlines didn't transition smoothly - they seemed to jump rather than flow seamlessly. To address this issue, I attempted to incorporate fadeIn and fadeOut functions which did improve the smoothness slightly. ...

Loading tab content on click using jQuery

These tabs have a chrome-like appearance. When the first tab (Facebook) is clicked, it shows Facebook content. Clicking on the second tab (Twitter) displays the content of the first tab. Tabs three, four, and five show their respective contents without any ...

Tips for extracting data from a JSON file

I'm attempting to retrieve a list of music genres from my json file using PHP and JQuery ajax. Here is the format of my json file [ "12-bar blues", "2 tone", "2-step garage", "4-beat", "50s progression", "a cappella", "accordion", " ...

The Vue 2 watch feature may encounter difficulties with mixin data properties, however, it functions properly when the data property belongs to the

I recently attempted to refactor some code by moving certain functionalities to a vue mixin from the component itself, with the intention of reusing it in multiple components. The simplified version of the code looks like this: export default { data() { ...

AmCharts Axis renderer mistakenly renders an additional grid line

I have successfully created a basic XYChart using amcharts4. To get rid of the grid lines on the x-axis, I changed the stroke opacity for the x-axis grid to 0 using the following code: xAxis.renderer.grid.template.strokeOpacity = 0; Everything was workin ...

unable to retrieve with express

Currently, I am in the process of developing an API that can maintain a log of users who utilize the application. I have successfully configured the login functionality to send usernames and rooms to the API via a POST request. However, I am facing challen ...

Implementing a function as the `data-*` attribute for an element in Angular 6

I have a question about my component.ts file: import { Component, OnInit, AfterViewInit, ElementRef, Inject } from '@angular/core'; import { DOCUMENT } from '@angular/platform-browser'; import { environment } from '../../../enviro ...

The animation for the accordion feature in the iOS Framework7-vue app seems to be moving at

I am encountering issues with the iOS app while developing both android and iOS apps with Framework7-vue. The Android app functions smoothly, but the iOS app is causing trouble. One of the features include a popup functionality with an accordion inside fo ...

Why does it fire off numerous requests even though I only called it once?

Everything seemed to be working fine with my project. However, I noticed in the console network that one of my GET requests is being sent twice even though I only triggered it once. View network console here If I comment out the entire created function co ...

Transform the snake code by incorporating a visual image as the face of the serpent

Can someone help me change the snake's face to an image instead of a color fill in this code? And how can I add arrows for mobile compatibility? (function() { // Insert JS code here })(); // Insert CSS code here This code snippet includes functi ...

Methods for dynamically adjusting content based on window width in Three.js

Currently, I have implemented a basic window resizing code in my project: function onWindowResize() { windowHalfX = window.innerWidth / 2; windowHalfY = window.innerHeight / 2; camera.aspect = window.innerWidth / window.innerHeight; came ...

Experiencing a problem with my regular expression query to MongoDB using Java

String searchQuery = "st"; BsonDocument regexFilter = Filters.regex("name", "^.*" + Pattern.quote(searchQuery), "i").toBsonDocument(null, null); userQuery.putAll(regexFilter); FindIterable<Document> allDocs = ...

Would it be beneficial to upload and download an image using the same ajax call?

I recently made a change to my web app's image upload feature. Previously, the process involved uploading an image, retrieving the image URL, and then making a second request to download the image from the URL. Now, I have streamlined it so that when ...

Limit the input to a specific format

Currently developing a JavaScript application, but having some confusion with Regular Expressions. The main goal is to allow users to input a specific format of strings in a text area for the application to process accordingly, thus requiring restriction o ...

Converting JSON data from ASP.NET MVC to a Canvas.js chart

Despite my efforts to find a solution by looking through similar posts, I am unable to resolve the issue. Within my asp.net MVC controller, I have a method called public object OfferChart() List<Det> obj = new List<Det>(); foreach ...

Stop CSS tooltip from overflowing outside of the page or window

One issue I am facing is with a CSS-only tooltip that uses a span to display the tooltip when hovering over a link. The problem arises when the link is positioned near the top or side of a page, causing the tooltip to extend beyond the edge of the page. I ...

Kudos to the information provided in the table!

My JSON data is structured like this: { description : "Meeting Description" name : "Meeting name" owner : { name: "Creator Name", email: "Creator Name" } } I want to present the details in a table format as follows: Meeti ...

How to format values in a textarea using AngularJS

Is there a way to address the following issue without replacing \n with other values? A user can input a description for something in a textarea and include line breaks. In the controller, there is a value called description which includes a string ...