include connectDB in route file
This commit is contained in:
@@ -2,9 +2,42 @@ import { User } from '../../../models/User';
|
|||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import { Webhook } from 'svix';
|
import { Webhook } from 'svix';
|
||||||
import connectDB from '../../lib/connectDB';
|
import connectDB from '../../lib/connectDB';
|
||||||
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
const CLERK_WEBHOOK_SECRET = process.env.CLERK_WEBHOOK_SECRET;
|
const CLERK_WEBHOOK_SECRET = process.env.CLERK_WEBHOOK_SECRET;
|
||||||
|
|
||||||
|
const DATABASE_URL = process.env.MONGO_URI;
|
||||||
|
|
||||||
|
let cached = global.mongoose;
|
||||||
|
|
||||||
|
if (!cached) {
|
||||||
|
cached = global.mongoose = { conn: null, promise: null };
|
||||||
|
}
|
||||||
|
async function connectDB() {
|
||||||
|
if (cached.conn) {
|
||||||
|
return cached.conn;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cached.promise) {
|
||||||
|
const opts = {
|
||||||
|
bufferCommands: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
cached.promise = mongoose.connect(DATABASE_URL, opts).then((mongoose) => {
|
||||||
|
console.log('MongoDB connected successfully');
|
||||||
|
return mongoose;
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error connecting to MongoDB:', error.message);
|
||||||
|
throw new Error('Error connecting to MongoDB');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cached.conn = await cached.promise;
|
||||||
|
return cached.conn;
|
||||||
|
}
|
||||||
|
|
||||||
export async function POST(req) {
|
export async function POST(req) {
|
||||||
console.log('Received request:', req);
|
console.log('Received request:', req);
|
||||||
|
|
||||||
|
|||||||
@@ -1,40 +0,0 @@
|
|||||||
import mongoose from "mongoose";
|
|
||||||
|
|
||||||
const DATABASE_URL = process.env.MONGO_URI;
|
|
||||||
|
|
||||||
if (!DATABASE_URL) {
|
|
||||||
throw new Error("Please define the DATABASE_URL environment variable inside .env.local");
|
|
||||||
}
|
|
||||||
|
|
||||||
let cached = global.mongoose;
|
|
||||||
|
|
||||||
if (!cached) {
|
|
||||||
cached = global.mongoose = { conn: null, promise: null };
|
|
||||||
}
|
|
||||||
|
|
||||||
async function connectDB() {
|
|
||||||
if (cached.conn) {
|
|
||||||
return cached.conn;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!cached.promise) {
|
|
||||||
const opts = {
|
|
||||||
bufferCommands: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
cached.promise = mongoose.connect(DATABASE_URL, opts).then((mongoose) => {
|
|
||||||
console.log('MongoDB connected successfully');
|
|
||||||
return mongoose;
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error connecting to MongoDB:', error.message);
|
|
||||||
throw new Error('Error connecting to MongoDB');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cached.conn = await cached.promise;
|
|
||||||
return cached.conn;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default connectDB;
|
|
||||||
Reference in New Issue
Block a user