Home > Blockchain >  Document appears to be null after MongoDB upload
Document appears to be null after MongoDB upload

Time:10-24

Or, at least, none of the values I submitted in the document are showing. Maybe I'm just looking in the wrong place. Here is my code:

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 is concerning app.post('/uploadShow') on line 50. After saving newShow, this is what the document looks like in MongoDB:

{"_id":{"$oid":"63532b09c95d57d1e52710ff"},"__v":{"$numberInt":"0"}}

No sign of the newShow values anywhere. Am I doing something wrong with the upload? Am I not looking in the right place for those values? Any advice would be appreciated.

CodePudding user response:

You are creating a new document without respecting the attributes in the schema declaration.
Also, don't mix await and then. Try with:

try {
  const newShow = await Show.create({
    eventTitle: req.body.eventTitle,
    location: req.body.location,
    date: newDate,
    time: newTime,
    ticket: req.body.ticket,
    desc: req.body.desc,
    image: req.body.image
  });
  res.status(200).send('FORM RECIEVED');
} catch (err) {
  res.status(400).send('ERROR');
}
  • Related