db progress
This commit is contained in:
@@ -1,11 +1,37 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from "next";
|
import { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { MongoClient } from "mongodb";
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
export default function handler(req: NextApiRequest, res: NextApiResponse){
|
const uri = process.env.MONGODB_URI || "mongodb://localhost:27017/mydatabase";
|
||||||
if(req.method === 'POST')
|
const clientOptions = { serverApi: { version: "1" as const, strict: true, deprecationErrors: true } };
|
||||||
const { codeword, contacts } = req.body;
|
|
||||||
|
|
||||||
try{
|
// 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" });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user