Mongodb seeding in nextjs 14

23 views Asked by At

I want to seed my static data to mongodb database using mongoose in my nextjs 14 project . I am using typescript for this project. I am saving two fields to the databse one for Users and second for Products. But when I try to do it my Users data is shown in the database but my products data is not getting uploaded . Need your help..

DbConnect.ts

import mongoose from "mongoose";

async function dbConnect() {
  try {
    await mongoose.connect(process.env.MONGODB_URI!);
  } catch (error) {
    throw new Error("Connection failed");
  }
}

export default dbConnect;

route.ts

import dbConnect from '@/lib/dbConnect'
import ProductModel from '@/lib/models/ProductModel'
import UserModel from '@/lib/models/UserModel'
import { NextRequest, NextResponse } from 'next/server'

export const GET = async (request: NextRequest) =\> {

const { users, products } = data
await dbConnect()

await UserModel.deleteMany()
await UserModel.insertMany(users)

await ProductModel.deleteMany()
await ProductModel.insertMany(products)

return NextResponse.json({
message: 'seeded successfully',
users,
products,
})
}

My mongodb compass database shows this: products users

I dont know the error I am facing ... your help will be appreciated . Thanks in advance.

I am trying to solve the mongodb data seeding procedure in nextjs 14 using mongoose. Seeding the users and products data in the database , the users data is stored in the database but my products data does'nt got uploaded on the databsase.

0

There are 0 answers