db progress

This commit is contained in:
BGV
2025-03-30 01:46:31 -04:00
parent a0ee0685dc
commit afddf08d3e

View File

@@ -1,11 +1,37 @@
import { NextApiRequest, NextApiResponse } from "next";
import { MongoClient } from "mongodb";
import mongoose from "mongoose";
export default function handler(req: NextApiRequest, res: NextApiResponse){
if(req.method === 'POST')
const { codeword, contacts } = req.body;
try{
const uri = process.env.MONGODB_URI || "mongodb://localhost:27017/mydatabase";
const clientOptions = { serverApi: { version: "1" as const, strict: true, deprecationErrors: true } };
// Create a reusable connection function
async function connectToDatabase() {
if (mongoose.connection.readyState === 0) {
// Only connect if not already connected
await mongoose.connect(uri, clientOptions);
console.log("Connected to MongoDB!");
}
}
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
// Ensure the database is connected
await connectToDatabase();
if (req.method === 'POST') {
const { codeword, contacts } = req.body;
// Perform database operations here
console.log("Codeword:", codeword);
console.log("Contacts:", contacts);
res.status(200).json({ success: true, message: "Data saved successfully!" });
} else {
res.setHeader('Allow', ['POST']);
res.status(405).end(`Method ${req.method} Not Allowed`);
}
} catch (error) {
console.error("Error:", error);
res.status(500).json({ success: false, error: "Internal Server Error" });
}
}