title: Unhandled promise rejection - Typescript wit hexpress and mongoose
body: <p>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:</p>
  <pre><code>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) =&gt; {
    res.send(eventApplication.getAll())
  })
  
  app.post('/event', async (req: any, res: any) =&gt; {
    await eventApplication.createAnEvent(req.body)
    res.json({
      success: true,
    })
  })
  
  app.post('/new-comp', urlEncodedParser, async (res: any, req: any) =&gt; {
    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)
  </code></pre>
  <p>I also have this file that has all my classes:</p>
  <pre><code>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 = () =&gt; this.place === 'Poznan' &amp;&amp; this.date === new Date()
  
    public getSubs = () =&gt; this.subscriptions
  
    public subscribe = (sub: Subscription) =&gt; {
      this.subscriptions = [...this.subscriptions, sub]
  
      return this
    }
  
    public cancelSubscription(subscription: Subscription) {
      const subExists = this.subscriptions.find(
        (it) =&gt; it.id === subscription.id &amp;&amp; it.name === subscription.name,
      )
      if (!subExists) {
        throw new Error('Subscription does not exist.')
      }
  
      this.subscriptions = this.subscriptions.filter(
        (it) =&gt; it.id !== subscription.id,
      )
    }
  }
  </code></pre>
  <br>
  Now my issue is that when I post some data to my app using curl, I have anerror message from the server as follows: 
  <blockquote>
  (node:3264) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'subscriptions' of undefined
  </blockquote>
  I 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