Skip to content
Snippets Groups Projects
Commit d1fcadac authored by Anna Granberg's avatar Anna Granberg
Browse files

funkar ej:( måste forska vidare

parent 5d9b8c81
No related branches found
No related tags found
No related merge requests found
import { dbConnect } from '../../../lib/db';
import { NextResponse } from 'next/server';
import { NextRequest } from "next/server";
import { NextApiRequest, NextApiResponse } from "next";
import dbConnect from "../../../lib/dbConnect";
import Movie from "../../../lib/myndigheter";
export async function GET(){
const con = await dbConnect();
return new NextResponse('connected');
}
\ No newline at end of file
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
const { method } = req;
await dbConnect();
if (method === "GET"){
try {
const movies = await Movie.find({});
res.status(200).json({ success: true, data: movies });
} catch (error) {
res.status(400).json({ success: false });
}
}
}
\ No newline at end of file
//CONNECTING TO OUR DATABASE
import mongoose from "mongoose";
declare global {
var mongoose: any;
}
const MONGODB_URI = process.env.MONGODB_URI!;
if (!MONGODB_URI) {
throw new Error(
"Please define the MONGODB_URI environment variable inside .env.local",
);
}
let cached = global.mongoose;
if (!cached) {
cached = global.mongoose = { conn: null, promise: null };
}
async function dbConnect() {
if (cached.conn) {
return cached.conn;
}
if (!cached.promise) {
const opts = {
bufferCommands: false,
};
cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
return mongoose;
});
}
try {
cached.conn = await cached.promise;
} catch (e) {
cached.promise = null;
throw e;
}
return cached.conn;
}
export default dbConnect;
\ No newline at end of file
import mongoose from "mongoose";
export interface Movie extends mongoose.Document {
_id: string;
title: string;
}
/* correspond to a collection in your MongoDB database. */
const MovieSchema = new mongoose.Schema<Movie>({
title: {
type: String,
required: [true, "Please provide a name for this pet."],
maxlength: [60, "Name cannot be more than 60 characters"],
},
});
const MovieModel = mongoose.models.Movie || mongoose.model<Movie>("Movie", MovieSchema);
export default MovieModel
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment