Colt / YelpCamp

574 stars 561 forks source link

Campground New & Create - POST not working #15

Closed mchtaylor closed 2 years ago

mchtaylor commented 2 years ago

After adding the POST method to make the "Add Campground" button works, nothing happens. No error or anything. I can't find anything wrong with the code. Please help!

`const express = require('express'); const path = require('path'); const mongoose = require('mongoose'); const Campground = require('./models/campground');

mongoose.connect('mongodb://localhost:27017/yelp-camp', { useNewUrlParser: true, // useCreateIndex: true, doesn't work in Mongoose 6 useUnifiedTopology: true })

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

const app = express();

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

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

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

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

app.get('/campgrounds/new', (req, res) => { res.render('campgrounds/new'); })

app.post('/campgrounds', async (req, res) => { const campground = new Campground(req.body.campground); await campground.save(); res.redirect(/campgrounds/${campground._id}) })

app.get('/campgrounds/:id', async (req, res) => { const campground = await Campground.findById(req.params.id) res.render('campgrounds/show', { campground }); })

app.listen(3000, () => { console.log('Serving on port 3000') })`

mchtaylor commented 2 years ago

I figured out what's wrong with my code! Apparently, instead of typing <form> in the new.ejs file, I typed <from>.