Variable showing as ‘undefined’ when I try to hit the route

I thought I had properly set up my CRUD for two different models (venues & artists) — venues works fine but when I try to hit my ‘artists/index’, I receive the error ‘Artists is not defined’.

As far as I can tell, I only need two things:

  1. Artist variable defined (7th row from top)
  2. A get request in the app.js (route syntax is listed in the very last grouping below)

I’ve confirmed my post requests work by validating there’s artist data in the database using the terminal, but I can’t get them to pull through the route.

Can you help me understand what I’m missing? It’s driving me nuts!

const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const ejsMate = require('ejs-mate');
const methodOverride = require('method-override');
const Venue = require('./models/venue');
const Artist = require('./models/artist');

const dbUrl = process.env.DB_URL || 'mongodb://localhost:27017/data-base';

mongoose.connect(dbUrl, {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true,
    useFindAndModify: false
});

const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
    console.log("Database connected");
});

const app = express();

app.engine('ejs', ejsMate);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'))

app.use(express.urlencoded({ extended: true }));
app.use(methodOverride('_method'));

app.get('/', (req, res) => {
    res.render('home')
})

app.get('/venues', async (req, res) => {
    const venues = await Venue.find({});
    res.render('venues/index', { venues })
});

app.get('/artists', async (req, res) => {
    const artists = await Artist.find({});
    res.render('artists/index', { artists })
});

I’m receiving the following error and you can see artists/index.ejs on my github

ReferenceError: /Users/chaseschlachter/mtapp/views/artists/index.ejs:8
<ul>
 <%  for (let artist of Artists) {%>
  <li><a href="/artists/<%= artist._id %>"><%= artist.name %>/a></li>
     <% } %> 
 </ul>

Artists is not defined

Answer

Probably you missed to include artists in res.render('artists/index', artists ).

As you need to specify in more detail your codebase.