Recently, I've been exploring the idea of integrating database switching into a NextJS application. While I am somewhat new to NextJS as a framework, I do have previous experience working with NodeJS and React. In a Node environment, this concept might look something like the following:
let db = cache.get(DB_NAME);
const Car = db.model('Car');
let allCars = await Car.find();
In this scenario, the cached value for DB_NAME essentially represents a connected specific database. However, during my attempts at implementation, I keep encountering an error that reads
Schema hasn't been registered for model "Car". Use mongoose.model(name, schema)
, despite having created the schema and imported it using require('../models/Car');
within my dbConnection.js file where all these actions are happening. For reference, here's a glimpse of my schema:
const mongoose = require('mongoose');
const CarSchema = new mongoose.Schema({
... schema stuff...
},
{ timestamps: true });
module.exports = mongoose.models.Car || mongoose.model('Car', CarSchema);
If anyone has encountered similar issues or has insights on how to navigate this challenge, I would greatly appreciate your input. Thank you!