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;