title: How to fix "TypeError: Cannot set property 'approved' of null"?
body: <p>So my code was all working fine, until on one of my application controllers I added some code, and now it's throwing me this error when I try to POST the data: </p>
  
  <pre><code>(node:10928) UnhandledPromiseRejectionWarning: TypeError: Cannot set
  property 'approved' of null
      at store (C:\Users\sadkevin\Desktop\Programs\Rocketseat\SemanaOmnistack9\backend\src\controllers\RejectionController.js:9:26)
      at process._tickCallback (internal/process/next_tick.js:68:7)
  (node:10928) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
  (node:10928) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  </code></pre>
  
  <p>This is my RejectionController, where I set if the bookings are false (and there is also a ApprovalController that only changes the false value of booking.approved to true, and it throws me the same error):</p>
  
  <pre><code>const Booking = require('../models/Booking');
  
  module.exports = {
      async store(req, res) {
          const { booking_id } = req.params;
  
          const booking = await Booking.findById(booking_id).populate('spot');
  
          booking.approved = false;
  
          await booking.save();
  
          return res.json(booking);
      }
  };
  </code></pre>
  
  <p>This is my bookings controller:</p>
  
  <pre><code>const Booking = require('../models/Booking');
  
  module.exports = {
      async store(req, res) {
          const { user_id } = req.headers;
          const { spot_id } = req.params;
          const { date } = req.body;
  
          const booking = await Booking.create({
              user: user_id,
              spot: spot_id,
              date,
          });
  
          await booking.populate('spot').populate('user').execPopulate();
  
          const ownerSocket = req.connectedUsers[booking.spot.user];
  
          if (ownerSocket) {
              req.io.to(ownerSocket).emit('booking_request', booking);
          }
  
          return res.json(booking);
      }
  };
  </code></pre>
  
  <p>This is the MongoDB schema for the bookings:</p>
  
  <pre><code>const mongoose = require('mongoose');
  
  const BookingSchema = new mongoose.Schema({
      date: String,
      approved: Boolean,
      user: {
          type: mongoose.Schema.Types.ObjectId,
          ref: 'User'
      },
      spot: {
          type: mongoose.Schema.Types.ObjectId,
          ref: 'Spot'
      }
  });
  
  module.exports = mongoose.model('Booking', BookingSchema);
  </code></pre>
  
  <p>And these are the routes of my application:</p>
  
  <pre><code>const express = require('express');
  const multer = require('multer');
  const uploadConfig = require('./config/upload');
  
  const SessionController = require('./controllers/SessionController');
  const SpotController = require('./controllers/SpotController');
  const DashboardController = require('./controllers/DashboardController');
  const BookingController = require('./controllers/BookingController');
  const ApprovalController = require('./controllers/ApprovalController');
  const RejectionController = require('./controllers/RejectionController');
  
  const routes = express.Router();
  const upload = multer(uploadConfig);
  
  routes.post('/sessions', SessionController.store);
  
  routes.get('/spots', SpotController.index);
  routes.post('/spots', upload.single('thumbnail'), SpotController.store);
  
  routes.get('/dashboard', DashboardController.show);
  
  routes.post('/spots/:spot_id/bookings', BookingController.store);
  
  routes.post('/bookings/:booking_id/approvals', ApprovalController.store);
  routes.post('/bookings/:booking_id/rejections', RejectionController.store);
  
  module.exports = routes;
  </code></pre>
  
  <p>Please help.</p>