How to fix "TypeError: Cannot set property 'approved' of null"?

how to fix anything
how to fix a computer
how to fix phone
how to fix things around the house
how to fix a zipper
how to fix keyboard
how to fix minecraft
how to repair appliances

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:

(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.

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):

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);
    }
};

This is my bookings controller:

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);
    }
};

This is the MongoDB schema for the bookings:

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);

And these are the routes of my application:

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;

Please help.


It's hard to say what the root cause of the error is, but the booking variable is set to null after returning from the Promise. Either the Booking is not found in the db or the booking_id is not passed in through the request params. I would recommend wrapping around your async await code in a try catch to help with debugging and see what the console error logged is.

async store(req, res) {
   try {
        const { booking_id } = req.params;

        const booking = await Booking.findById(booking_id).populate('spot');

        booking.approved = false;

        await booking.save();

        return res.json(booking);

    } catch (err) {

        console.error(err);

    }
}

28 HACKS TO FIX ANYTHING QUICKLY, holes in plywood, how to unscrew a rusty screw, how to drill a hole in the wood, and other tricks Duration: 9:51 Posted: Nov 10, 2018 iFixit is a global community of people helping each other repair things. Let's fix the world, one device at a time. Troubleshoot with experts in the Answers forum—and build your own how-to guides to share with the world.


Try changing your approved: Boolean to approved: Boolean? in your MongoDB schema

this will allow it to be nullable

How to Fix Everything, Umbra visits the Fixers' Collective in Brooklyn, a social experiment in improvisational fixing Duration: 2:53 Posted: Mar 9, 2010 Easy Fix solutions have been discontinued. Instead, you can upgrade to Windows 10 and get troubleshooters that are built right in. To get Windows 10, see Windows 10 Home .


Found the problem, at Insomnia I was not passing the booking ID, that's why it couldn't set property of null.

How To FIX YOUR PHONE In SECONDS! (Reacting To Life Hacks , How To FIX YOUR PHONE In SECONDS! If you enjoyed this video, watch more here: Duration: 11:40 Posted: Oct 9, 2019 Repairing a Computer that Freezes or Runs Slowly 1. Back up your files. A computer that freezes or runs slowly could be a sign of more serious problems to come. 2. Give your computer a bit of rest. If you leave your computer on all the time, you can often fix a problem by turning 3. Update your


Popular Mechanics How to Fix Anything: Essential Home Repairs , is the handy and reliable go-to guide for the most common household problems offering a primer on plumbing, unexpected hacks like using a How To Repair and Restore Windows 10 Method 1: Use Windows Startup Repair. If Windows 10 is unable to boot up and takes you to the login screen or the Method 2: Use Windows Restore. Complete step 1 from the previous method to get to Windows 10's Advanced Startup Options Method 3: Perform a


iFixit: The Free Repair Manual, iFixit is a global community of people helping each other repair things. Let's fix the world, one device at a time. Troubleshoot with experts in the Answers  Fix.com is a one-stop source for fixing products in and around your home. Millions of quality OEM replacement parts, repair videos, instructions, and same-day shipping available!


Repair Manuals for Every Thing, Thousands of repair manuals, tutorials, and how-to guides for DIY fixes. From electronics to cars, learn how to repair your own stuff and save yourself some  To fix a bad credit score, understand the basic contributors to credit—including whether you pay your bills on time and whether you carry balances on credit cards—and identify the factors that are making a negative impact. Checking for errors on your credit report is also an important step.