It seems that none of the values I entered in the form are appearing in the document. Possibly, I'm searching in the incorrect location. Check out my code below:
import mongoose from 'mongoose'
import express from 'express'
import * as dotenv from 'dotenv'
import cors from 'cors'
import bodyParser from 'body-parser'
import MailJet from 'node-mailjet'
// SUGGESTED FIX for: '__dirname is not defined in ES module scope'
import path from 'path'
import { fileURLToPath } from 'url'
// Create Show schema and model
const showSchema = new mongoose.Schema({
eventTitle: String,
location: String,
date: String,
time: String,
ticket: String,
desc: String,
image: String
})
const Show = mongoose.model('Show', showSchema)
// SUGGESTED FIX for: '__dirname is not defined in ES module scope'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
dotenv.config()
const app = express()
const port = process.env.PORT
// Set CORS and connect
app.use(cors())
app.listen(port, () => console.log(`Server listening on port ${port}`))
mongoose.connect(process.env.MONGOOSE_CONNECT)
// Create storage engine
const upload = multer()
// MailJet configuration
const mailjet = MailJet.apiConnect(
process.env.MAIL_KEY,
process.env.MAIL_SECRET
)
// Upload new show
app.post('/uploadShow', upload.single(), async function (req, res) {
// Convert date
const dateParts = req.body.date.split('-')
dateParts.push(dateParts.shift())
const newDate = dateParts.join('-')
// Convert time
const timeParts = req.body.time.split(":")
const daylight = timeParts[0] < 12 ? " AM" : " PM"
timeParts[0] !== "12" && parseInt(timeParts[0]) !== 0
? timeParts.unshift(timeParts.shift() % 12)
: timeParts[0] = "12"
const newTime = timeParts.join(":") + daylight
// ACCESS BODY, UPLOAD TO MONGODB DATABASE
const newShow = new Show({
TITLE: req.body.eventTitle,
LOCATION: req.body.location,
DATE: newDate,
TIME: newTime,
TICKET: req.body.ticket,
DESC: req.body.desc,
IMAGE: req.body.image
})
await newShow.save().then(console.log('SHOW SAVED')).catch(err => console.log(err))
res.status(200)
res.send("FORM RECIEVED")
})
app.post('/contact', bodyParser.json(), function(req, res){
console.log(req.body)
const request = mailjet.post('send', { version: 'v3.1' }).request({
Messages: [
{
From: {
Email: process.env.SENDER_EMAIL,
Name: 'KMAC Music'
},
To: [
{
Email: process.env.RECIPIENT_EMAIL,
Name: 'KMAC Music Contact Inbox'
}
],
Subject: `NEW CONTACT from ${req.body.name}`,
TextPart: `${req.body.name} writes regarding ${req.body.reason}:\n\n\t${req.body.message}\n\nCONTACT INFO:\n\t${req.body.email}\n\t${req.body.phone}`,
HTMLPart: null,
},
],
})
request
.then(result => {
console.log(result.body)
})
.catch(err => {
console.log(err.statusCode)
})
})
app.post('/booking', bodyParser.json(), function(req, res){
console.log(req.body)
let day = new Date(req.body.date).toLocaleDateString('en-us', {weekday: "long", year: "numeric", month: "short", day: "numeric"})
const timeFunc = (dateString) => {
const timeArr = dateString.split(':')
const hour = Number(timeArr[0]) % 12
const ampm = Number(timeArr[0]) < 12 || Number(timeArr[0]) === 24 ? 'AM' : 'PM'
return `${hour}:${timeArr[1]} ${ampm}`
}
const request = mailjet.post('send', { version: 'v3.1' }).request({
Messages: [
{
From: {
Email: process.env.SENDER_EMAIL,
Name: 'KMAC Music'
},
To: [
{
Email: process.env.RECIPIENT_EMAIL,
Name: 'KMAC Music Contact Inbox'
}
],
Subject: `NEW bOOKING REQUEST from ${req.body.name}`,
TextPart: `${req.body.name} would like to book your services!\n\n\tDATE: ${day}\n\tTIME: ${timeFunc(req.body.time)}\n\tDESCRIPTION:\n\t\t${req.body.description}\n\n\tCONTACT INFO:\n\t\t${req.body.email}\n\t\t${req.body.phone}`,
HTMLPart: null,
},
],
})
request
.then(result => {
console.log(result.body)
})
.catch(err => {
console.log(err.statusCode)
})
})
This issue pertains to app.post('/uploadShow')
line 50. After saving newShow
, here's how the MongoDB document looks:
{"_id":{"$oid":"63532b09c95d57d1e52710ff"},"__v":{"$numberInt":"0"}}
The values for newShow
are missing. Could there be an error with the upload process? Or perhaps the data isn't where it should be? Any guidance on this matter would be greatly appreciated.