我以为我已经为两个不同的模型(场地和艺术家(正确设置了CRUD——场地运行良好,但当我尝试点击"艺术家/索引"时,我收到错误"艺术家未定义"。
据我所知,我只需要两件事:
- 艺术家变量已定义(从顶部起第7行(
- app.js中的get请求(路由语法在下面最后一组中列出(
我已经通过使用终端验证数据库中是否有艺术家数据来确认我的帖子请求有效,但我无法让他们通过路线。
你能帮我理解我错过了什么吗?它把我逼疯了!
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 })
});
我收到以下错误,你可以在我的github 上看到艺术家/index.ejs
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
可能您错过了在res.render('artists/index', artists )
中包含artists
。
因为您需要更详细地指定您的代码库。