For some reason, the Model I defined is not working properly, despite being similar to another one that works without errors. Can anyone help me figure out why?
If you need to see a minimal, reproducible example, check it out here.
The problematic code:
import mongoose from 'mongoose';
const TokenSchema = new mongoose.Schema({
_userId: { type: mongoose.Schema.Types.ObjectId, required: true, ref: 'User' },
token: { type: String, required: true },
createdAt: { type: Date, required: true, default: Date.now, expires: 43200 }
});
export default mongoose.models.Token || mongoose.model('Token', TokenSchema);
The working code:
import mongoose from 'mongoose';
import emailValidator from 'email-validator'
import bcrypt from 'bcrypt'
import crypto from 'crypto'
const SALT_ROUNDS = 12;
const UserSchema = new mongoose.Schema(
{
// Schema definition here
},
{
timestamps: true
}
);
// Methods and hooks definitions here
export default mongoose.models.User || mongoose.model('User', UserSchema)
I'm using this example from the Next.js repository as a reference.
Any assistance would be greatly appreciated!