mirror of
https://github.com/SkalaraAI/waitlist.git
synced 2025-04-09 21:20:15 -04:00
fix errors
This commit is contained in:
parent
353c075007
commit
60183a37c5
|
@ -1,4 +1,5 @@
|
||||||
import { NextResponse, NextRequest } from "next/server";
|
import { NextResponse, NextRequest } from "next/server";
|
||||||
|
import { MongoClient } from "mongodb";
|
||||||
import client from "@/lib/db";
|
import client from "@/lib/db";
|
||||||
|
|
||||||
export async function POST(req: NextRequest, res: NextResponse) {
|
export async function POST(req: NextRequest, res: NextResponse) {
|
||||||
|
@ -7,7 +8,7 @@ export async function POST(req: NextRequest, res: NextResponse) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const dbClient = await client.promise;
|
const dbClient = (await client.promise) as MongoClient;
|
||||||
const db = dbClient.db("waitlist_db"); // Replace <dbname> with your actual db name
|
const db = dbClient.db("waitlist_db"); // Replace <dbname> with your actual db name
|
||||||
const email = (await req.json()) as any;
|
const email = (await req.json()) as any;
|
||||||
const emailText = email.email;
|
const emailText = email.email;
|
||||||
|
|
|
@ -13,7 +13,7 @@ export default function Home() {
|
||||||
const [alertType, setAlertType] = useState("default");
|
const [alertType, setAlertType] = useState("default");
|
||||||
const [alertMessage, setAlertMessage] = useState("");
|
const [alertMessage, setAlertMessage] = useState("");
|
||||||
const [submitted, setSubmitted] = useState(false);
|
const [submitted, setSubmitted] = useState(false);
|
||||||
async function handleSubmit(e) {
|
async function handleSubmit(e: any) {
|
||||||
e.preventDefault(); // prevent the default form submission behavior
|
e.preventDefault(); // prevent the default form submission behavior
|
||||||
const res = await fetch("/api/waitlist", {
|
const res = await fetch("/api/waitlist", {
|
||||||
method: "POST", // specify the HTTP method
|
method: "POST", // specify the HTTP method
|
||||||
|
@ -81,11 +81,7 @@ export default function Home() {
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
{alertMessage && (
|
{alertMessage && (
|
||||||
<Alert
|
<Alert variant="default" className="w-1/2">
|
||||||
variant={alertType}
|
|
||||||
className="w-1/2"
|
|
||||||
onClose={() => setAlertMessage("")}
|
|
||||||
>
|
|
||||||
{alertType === "success" ? (
|
{alertType === "success" ? (
|
||||||
<Check className="w-6 h-6" />
|
<Check className="w-6 h-6" />
|
||||||
) : (
|
) : (
|
||||||
|
|
8
global.d.ts
vendored
Normal file
8
global.d.ts
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import { MongoClient } from "mongodb";
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
var mongo: {
|
||||||
|
conn: MongoClient | null;
|
||||||
|
promise: Promise<MongoClient> | null;
|
||||||
|
};
|
||||||
|
}
|
35
lib/db.js
35
lib/db.js
|
@ -1,35 +0,0 @@
|
||||||
// lib/db.js
|
|
||||||
import { MongoClient } from "mongodb";
|
|
||||||
|
|
||||||
const uri = process.env.MONGODB_URI; // Connection string from .env.local
|
|
||||||
const options = {
|
|
||||||
useUnifiedTopology: true,
|
|
||||||
useNewUrlParser: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
let client;
|
|
||||||
let clientPromise;
|
|
||||||
|
|
||||||
if (!process.env.MONGODB_URI) {
|
|
||||||
throw new Error("Please add your MongoDB URI to .env.local");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.env.NODE_ENV === "development") {
|
|
||||||
// In development mode, use a global variable to keep the database connection
|
|
||||||
// open across hot reloads
|
|
||||||
if (!global.mongo) {
|
|
||||||
global.mongo = { conn: null, promise: null };
|
|
||||||
}
|
|
||||||
client = global.mongo;
|
|
||||||
} else {
|
|
||||||
// In production mode, create a new connection for every request
|
|
||||||
client = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!client.promise) {
|
|
||||||
client.promise = MongoClient.connect(uri, options).then((mongoClient) => {
|
|
||||||
return mongoClient;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export default client;
|
|
21
lib/db.ts
Normal file
21
lib/db.ts
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
import { MongoClient } from "mongodb";
|
||||||
|
|
||||||
|
const uri = process.env.MONGODB_URI as string; // Connection string from .env.local
|
||||||
|
|
||||||
|
if (!process.env.MONGODB_URI) {
|
||||||
|
throw new Error("Please add your MongoDB URI to .env.local");
|
||||||
|
}
|
||||||
|
|
||||||
|
let client =
|
||||||
|
process.env.NODE_ENV === "development"
|
||||||
|
? global.mongo
|
||||||
|
: { conn: null, promise: null };
|
||||||
|
|
||||||
|
if (!client.promise) {
|
||||||
|
client.promise = MongoClient.connect(uri).then((mongoClient) => {
|
||||||
|
client.conn = mongoClient;
|
||||||
|
return mongoClient;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export default client;
|
Loading…
Reference in New Issue
Block a user