0

I'm trying to set up routing for a simulated "rental" business in Node using express and mongoose.

My backend so far works fine in that I can save customers, movies, and so on in the database. However, when I try to hook up the routing for rentals I get this error:

message: 'Cannot overwrite Customer model once compiled.',

I believe the problem is due to my attempting to embed customer data inside of the rental document, but I'm not sure.

Problem Code:

const { Rental, validateRental } = require("../Models/rental");
const { Movies } = require("../Models/movie");
const { Customers } = require("../Models/customer");
const express = require("express");
const router = express.Router();

////////////////////
//! [C]-RUD
////////////////////

router.post("/", async (req, res) => {
  const { error } = validateRental(req.body);
  if (error) return res.status(400).send(error.details[0].message);

  const customer = await Customers.findById(req.body.customerId);
  if (!customer) return res.status(400).send("Invalid customer.");

  const movie = await Movies.findById(req.body.movieId);
  if (!movie) return res.status(400).send("Invalid movie.");

  if (movie.numberInStock === 0)
    return res.status(400).send("Movie not in stock.");

  let rental = new Rental({
    renter: {
      _id: customer._id,
      name: customer.name,
      phone: customer.phone
    },
    movie: {
      _id: movie._id,
      title: movie.title,
      dailyRentalRate: movie.dailyRentalRate
    }
  });
  rental = await rental.save();

  movie.numberInStock--;
  movie.save();

  res.send(rental);
});

////////////////////////
//! Exports
////////////////////////
module.exports = router;

And my Rentals mongoose model looks like this:

const Rentals = mongoose.model(
  "Rental",
  new mongoose.Schema({
    renter: {
      type: new mongoose.Schema({
        name: {
          type: String,
          required: true,
          minlength: 5,
          maxlength: 50
        },
        isGold: {
          type: Boolean,
          default: false
        },
        phone: {
          type: String,
          required: true,
          minlength: 5,
          maxlength: 50
        }
      }),
      required: true
    },
    movie: {
      type: new mongoose.Schema({
        title: {
          type: String,
          required: true,
          trim: true,
          minlength: 5,
          maxlength: 255
        },
        dailyRentalRate: {
          type: Number,
          required: true,
          min: 0,
          max: 255
        }
      }),
      required: true
    },
    dateOut: {
      type: Date,
      required: true,
      default: Date.now
    },
    dateReturned: {
      type: Date
    },
    rentalFee: {
      type: Number,
      min: 0
    }
  })
); 

Finally, my customer.js looks like this:

const mongoose = require("mongoose");
const Joi = require("@hapi/joi");
const CustomJoi = Joi.extend(require("joi-phone-number"));

// * ----------  PRE VALIDATE CUSTOMER NAME and PHONE NUMBER ----------
function validateCustomer(customer) {
  const schema = CustomJoi.object({
    name: CustomJoi.string()
      .min(2)
      .max(30)
      .trim()
      .required(),
    phone: CustomJoi.string()
      .phoneNumber({ defaultCountry: "US", strict: true })
      .trim()
      .required(),
    isGold: CustomJoi.boolean()
  });

  return schema.validate(customer);
}

const customerSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    minlength: 2,
    maxlength: 30,
    trim: true
  },
  phone: {
    type: String,
    required: true,
    trim: true,
    minlength: 5,
    maxlength: 50
  },
  isGold: {
    type: Boolean,
    default: false
  }
});

//* Define customers model (moved the schema declaration into it.)
const Customers = mongoose.model("Customer", customerSchema);

exports.Customers = Customers;
exports.validate = validateCustomer;
  • You might want to check out the solutions suggested in this thread: https://stackoverflow.com/questions/19051041/cannot-overwrite-model-once-compiled-mongoose most especially the one about letter-casing. – Tunmee Mar 03 '20 at 13:43
  • Okay, I managed to get this working by editing my customer.js but it feels "wrong" somehow. Not a best practice? original code: `const Customers = mongoose.model("Customer", customerSchema)` New Code: `let Customers; try { Customers = mongoose.model("Customer"); } catch (error) { Customers = mongoose.model("Customer", customerSchema); }` – Turkinolith Mar 03 '20 at 19:16

1 Answers1

0

Okay after testing things for a day it looks like the answer I ended up using by putting the whole model declaration within a try/catch seemed to do the trick with no obvious side effects. I haven't done a performance analysis, but that is beyond the scope of my tutorial anyhow and I'll address that in the future.

let Customers;
 try { Customers = mongoose.model("Customer");
 } catch (error) {
 Customers = mongoose.model("Customer", customerSchema);
 }