Please bear with me, I am a beginner in node and async stuff is still no super clear for me. I have the below piece of code and I a now working on the last part - the /new-comp route. It is supposed to post in the database I connected above:
import { Schema } from 'mongoose'
export const mongoose = require('mongoose')
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const urlEncodedParser = bodyParser.urlencoded({ extended: false })
mongoose.connect('mongodb://localhost:27017/CompetitionEvent')
export const db = mongoose.connection
db.on('error', console.error.bind(console, 'An error has occured: '))
db.once('open', function () {
  console.log('Connected to Mongodb')
})
const CompetitionSchema = new Schema({
  id: String,
  place: String,
  time: String,
  subscriptions: [],
  date: Date,
  cost: {
    currency: String,
    amount: Number,
  },
})
const CompetitionModel = mongoose.model('CompetitionModel', CompetitionSchema)
app.use(bodyParser.json())
app.get('/events', (_req: any, res: any) => {
  res.send(eventApplication.getAll())
})
app.post('/event', async (req: any, res: any) => {
  await eventApplication.createAnEvent(req.body)
  res.json({
    success: true,
  })
})
app.post('/new-comp', urlEncodedParser, async (res: any, req: any) => {
  await eventApplication.createAnEvent(req.body)
  const newComp = CompetitionModel(req.body)
  newComp.save(function (error: any, data: any) {
    if (error) throw error
    res.json(data)
  })
})
app.listen(8000)
I also have this file that has all my classes:
export interface Subscription {
  id: string
  event_id: string
  name: string
  surname: string
}
export interface EventDTO {
  id: string
  place: string
  time: string
  subscriptions: Subscription[]
  date: Date
  cost: EventCost
}
export interface EventCost {
  amount: number
  currency: string
}
export class CompetitionEvent {
  public subscriptions: Subscription[]
  public place: string
  public time: string
  public date: Date
  public cost: EventCost
  public id: string
  static save: any
  constructor(data: EventDTO) {
    this.subscriptions = data.subscriptions
    this.place = data.place
    this.time = data.time
    this.date = data.date
    this.cost = data.cost
    this.id = data.id
  }
  public isCompleted = () => this.place === 'Poznan' && this.date === new Date()
  public getSubs = () => this.subscriptions
  public subscribe = (sub: Subscription) => {
    this.subscriptions = [...this.subscriptions, sub]
    return this
  }
  public cancelSubscription(subscription: Subscription) {
    const subExists = this.subscriptions.find(
      (it) => it.id === subscription.id && it.name === subscription.name,
    )
    if (!subExists) {
      throw new Error('Subscription does not exist.')
    }
    this.subscriptions = this.subscriptions.filter(
      (it) => it.id !== subscription.id,
    )
  }
}
Now my issue is that when I post some data to my app using curl, I have anerror message from the server as follows:
(node:3264) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'subscriptions' of undefinedI am not sure how to understand this log. It seems that I have an unhandled promise somewhere (I get lines in the log but sometimes it points to empty lines in my program". Do you have any idea how I should understand / manage this issue? Thanks in advance