I'm trying to add db switching into a NextJS app. I'm new to NextJS as a framework but have experience with NodeJS and React. In Node it would be something like this:
let db = cache.get(DB_NAME);
const Car = db.model('Car');
let allCars = await Car.find();
Where the cached value for DB_NAME is just the cached connection to a specific db. However, I keep getting the error Schema hasn't been registered for model "Car". Use mongoose.model(name, schema) even though I created the schema and required it with require('../models/Car'); in my dbConnection.js file where all of this is taking place. Just because someone will probably ask, here's my schema:
const mongoose = require('mongoose');
const CarSchema = new mongoose.Schema({
... schema stuff...
},
{ timestamps: true });
module.exports = mongoose.models.Car || mongoose.model('Car', CarSchema);
Does anyone have any experience with this? Thanks!
Figured it out. As long as you require the models, it's just db.models.Car instead of db.model('Car')