Parsing JSON data into a template using a route for faster performance

I am having difficulty extracting data from a mongodb using a specific route. My objective is to retrieve the title fields from each object.

Below is the schema:

var mongoose  = require('mongoose');
var Schema    = mongoose.Schema;

var GiveSchema   = new Schema({
        title: String,
        shortname: String,
        contents: String,
        image: String,
        category: String
    });

module.exports = mongoose.model('GiveData',  GiveSchema);

The schema is stored in this variable:

var Givedata = mongoose.model( 'GiveData' );

This is my route:

app.get('/', function(req, res) {
    res.render('index.ejs',{
      list: Givedata.title,
      bootstrappedUser: req.user,
      something: req.body,
      page: 'home'
    });
});

I'm applying this logic in my template but getting back 'undefined'

<% for(var i=0; i< list.length; i++) { %>
    <a href="/"><li><%= list[i] %></li></a>
    <% } %> 

Answer №1

To get EJS functioning properly, you need to designate ejs as your view engine:

app.set('view engine', 'ejs');

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

Send multipart form data to a different server via pipe

I need assistance with handling a POST request on my Node Express server for uploading images through multipart form data. Currently, my Express app is set up to use body parser which does not support multipart bodies and suggests using alternative librari ...

Having trouble configuring the routes on my MEAN Stack application

I'm currently working on a basic application using the MEAN stack, but I'm facing issues with my routes. When I access localhost:3000, it redirects me to the 404 error page instead of displaying the posts at localhost:3000/#/home as expected. It ...

Developed specifically for asynchronous messaging, this Node.js REST API wrapper simplifies

When working with an event-driven microservice architecture that utilizes asynchronous messaging, what options exist for creating a 'synchronous' REST API wrapper where requests wait for response events before providing a result to the client? F ...

The Socket.io instance is missing sockets, resulting in an undefined state

Currently in the process of learning and experimenting with real-time chat systems. I'm attempting to construct a basic one. Here is the main section of my code: var express = require('express'); var http = require('http'); var f ...

Unable to utilize parseInt() function on LocalHost NodeJS with Body Parser

As a newcomer to node.js, I encountered an error while using parseInt or Number() in my server. Here is the code snippet: const express = require("express"); const app = express(); const bodyParser = require("body-parser"); app.use(bodyParser.urlencoded( ...

What is the best way to retrieve socket emitted data in a separate route in Express?

I'm currently working on a project that involves three specific files: index.html, result.html, and app.js. While I have successfully been able to emit data on button click and see it printed on the server, I am struggling to retrieve the value on res ...

Fetching the body in router.post seems to be problematic, but it can be easily achieved by using app.post in Node

I've encountered an issue with my express app. My goal is to have a form in handlebars and register a user based on this tutorial: , but I'm adapting it for a web application. Here's how I created the form: <div style="text-align:center ...

Can Express handle more than one GET query in a single route?

Currently, I am faced with a small issue that I'm unsure if it's even solvable. I am in the process of developing a website where users can store their contacts. I would like to be able to retrieve a contact by its ID using the URL format /conta ...

Node.js - Retrieving POST request parameters and directing users in an Express application

I encountered this specific issue while setting up a post endpoint for my nodejs CLI script that utilizes express to handle requests. app.use( express.static( path.format({dir: __dirname, base: 'client/dist'})) ); app.use( express ...

Utilizing MongoDB Data in HBS Template Rendering

Attempting to showcase data from my mongodb backend on an hbs template by following this tutorial. No errors are shown in server logs or browser console, but no data is visible on the template. Interestingly, it generates HTML markup. Seeking assistance to ...

Socket IO issue: CORS policy is blocking access to XMLHttpRequest

My application recently encountered an error. "Access to XMLHttpRequest at '' from origin '' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource." When conn ...

Troubleshooting issue with Express.json() functionality in the latest release of version 4.17

I'm currently exploring the MEAN stack and I am focused on performing CRUD operations. However, when I send data in the request body from Angular to the server, I end up receiving an empty request body. I'm unsure of where I might be making a mis ...

Struggling to grasp the concept of an Express server in Node.js

As I was following some online tutorials on setting up a Node server using Express 4, I wanted to simplify my question for better understanding. The main app.js file contains the following code (excluding other middleware lines) var express = require(&ap ...

Assurance of access granted through token

Currently, I am working with jwt authentication that involves access and refresh tokens. However, a problem arises when the access token expires and a new access token is generated on the frontend (react). This process returns a promise. In the backend/ro ...

The parameter passed to the mongoose.connect() method should be a string type

Encountering an issue with mongoose.connect() where I'm receiving the following error: Error: The "url" argument must be of type string. Received undefined {"data":{"code":"ERR_INVALID_ARG_TYPE"}} This is how I am using ...

Tips for ensuring old logs do not display on Heroku Logs

I'm puzzled as to why my heroku logs command keeps showing old logs. Attempting to resolve this issue, I tried: heroku drains heroku logs However, the logs still display outdated information: app[api]: Release v1 created by user <a href="/cdn-c ...

Navigating Passport's error handling in node.jsExploring error handling techniques

While I have delved into the intricacies of error handling in Node.js through this particular question titled "Error handling principles for Node.js + Express.js applications?", there remains a sense of uncertainty surrounding how passport handles authenti ...

Display or conceal the options to sign in and sign out

I am having issues with button visibility based on user authentication in my MySQL and EJS setup. connection.js const mysql = require('mysql'); const util = require('util'); require('dotenv').config(); const connection = mys ...

Navigating routes with Express js

Within my application, I have configured the static folder containing an 'index.html' file. I am looking to display this file on the route /profile Could this be done? ...

Express server is having trouble recognizing a specific route (simple issue)

Just starting to dive into Node.js. I managed to set up a basic server, but whenever I try to access localhost:3000 it just keeps loading indefinitely. var express = require('express'); var path = require('path'); var bodyParser = re ...