Currently, my form successfully posts data to an mLab-hosted database, but the only information that appears is an id number and `"__v": 0'. The form consists of five input fields with corresponding names based on a defined schema. Any suggestions on how to resolve this issue?
Below is the form structure:
<form action="/events" method="POST">
<fieldset>
<legend>New Event</legend>
<div class="form-1">
<div>
<label for="title">Title</label>
<input type="text" id="title" name="title" placeholder="Whale Watching">
</div>
<div>
<label for="date">Date</label>
<input type="text" id="date" name="date" placeholder="7/13">
</div>
<div>
<label for="date">Time</label>
<input type="text" id="time" name="time" placeholder="9:00am">
</div>
<div>
<label for="price">Price</label>
<input type="text" id="price" name="price" placeholder="ex. $150">
</div>
<div>
<label for="capacity">Capacity</label>
<input type="text" id="capacity" name="capacity" placeholder="ex. 12">
</div>
</div>
<button type="submit">Create Event</button>
</fieldset>
Defined model:
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const Schema = mongoose.Schema;
const EventSchema = new Schema({
title: String,
time: Number,
date: Date,
price: Number,
capacity: Number,
});
const Event = mongoose.model('events', EventSchema);
module.exports = Event;
Associated POST route:
router.post('/events', (req, res) => {
Event.create(req.body).then(function(events){
res.send(events);
});
});
Server setup:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const routes = require('./src/routes/index');
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(bodyParser.json());
app.use(express.static('public'));
app.use(routes);
mongoose.connect('mongodb://junk:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6ccd3c8cde6c2d597929794929488cbcac7c488c5c9cb">[email protected]</a>:41242/alaska-events');
app.listen(3000, () => {
console.log('listening on 3000')
});