I can’t add collection to mongodb database…. does anyone know why this is happening?
I use clerk and it works fine, I can see the users.
lib/database/index.tsx:
import mongoose from "mongoose";
const MONGODB_URI = process.env.MONGODB_URI;
let cached = (global as any).mongoose || { conn: null, promise: null };
export const connectToDatabase = async () => {
if (cached.conn) return cached.conn;
if (!MONGODB_URI) throw new Error("MONGODB_URI is missing");
cached.promise =
cached.promise ||
mongoose.connect(MONGODB_URI, {
dbName: "evently",
bufferCommands: false,
});
cached.conn = await cached.promise;
return cached.conn;
};
lib/database/user.model.ts:
import mongoose, { model, models, Schema } from "mongoose";
const UserSchema = new mongoose.Schema({
clerkId: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
username: { type: String, required: true, unique: true },
firstName: { type: String, required: true },
lastName: { type: String, required: true },
photo: { type: String, required: true },
});
const User = mongoose.models.User || mongoose.model("User", UserSchema);
export default User;
.env:
MONGODB_URI=mongodb+srv://patryk1:[email protected]/?retryWrites=true&w=majority&appName=Cluster0
Thx!
I used this tutorial: https://www.youtube.com/watch?v=zgGhzuBZOQg&list=PL6QREj8te1P7gixBDSU8JLvQndTEEX3c3&index=12
New contributor
Patryk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.