In hello.js
import connectMongo from '../../../util/connectDB';
import UserModel from '../../../models/UserModel';
import { NextResponse } from 'next/server'
export async function GET(request) {
return NextResponse.json({hello: 'Hello, Next.js!'})
}
export async function POST(request) {
const data = await request.json();
console.log(data);
return NextResponse.json(data)
}
In util/connextDB
import mongoose from 'mongoose';
const connectMongo = () => {
try {
const conn = mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});
console.log(`MongoDB Connected: ${conn.connection.host}`);
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}
};
export default connectMongo;
And the error shows
error – node_modules/mongoose/lib/types/objectid.js (21:31) @ prototype
error – TypeError: Cannot read properties of undefined (reading ‘prototype’)
at eval (webpack-internal:///(sc_server)/./node_modules/mongoose/lib/types/objectid.js:15:36)
at Object.(sc_server)/./node_modules/mongoose/lib/types/objectid.js (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/app/api/hello/route.js:2728:1)
at webpack_require (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/webpack-runtime.js:33:43)
at eval (webpack-internal:///(sc_server)/./node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:7:18)
at Object.(sc_server)/./node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/app/api/hello/route.js:451:1)
at webpack_require (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/webpack-runtime.js:33:43)
at eval (webpack-internal:///(sc_server)/./node_modules/mongoose/lib/drivers/node-mongodb-native/index.js:4:22)
at Object.(sc_server)/./node_modules/mongoose/lib/drivers/node-mongodb-native/index.js (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/app/api/hello/route.js:473:1)
at webpack_require (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/webpack-runtime.js:33:43)
at eval (webpack-internal:///(sc_server)/./node_modules/mongoose/lib/index.js:4:100)
at Object.(sc_server)/./node_modules/mongoose/lib/index.js (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/app/api/hello/route.js:2057:1)
at webpack_require (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/webpack-runtime.js:33:43)
at eval (webpack-internal:///(sc_server)/./node_modules/mongoose/index.js:5:18)
at Object.(sc_server)/./node_modules/mongoose/index.js (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/app/api/hello/route.js:242:1)
at webpack_require (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/webpack-runtime.js:33:43)
at eval (webpack-internal:///(sc_server)/./models/UserModel.js:5:66)
at Module.(sc_server)/./models/UserModel.js (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/app/api/hello/route.js:154:1)
at webpack_require (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/webpack-runtime.js:33:43)
at eval (webpack-internal:///(sc_server)/./app/api/hello/route.js:6:75)
at Module.(sc_server)/./app/api/hello/route.js (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/app/api/hello/route.js:143:1)
at webpack_require (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/webpack-runtime.js:33:43)
at eval (webpack-internal:///(sc_server)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fhello%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Fhello%2Froute.js&appDir=%2FUsers%2Favinashsethu%2FDesktop%2FDevelopment%2Fekali-lms%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2FUsers%2Favinashsethu%2FDesktop%2FDevelopment%2Fekali-lms&isDev=true&tsconfigPath=tsconfig.json&assetPrefix=!:13:130)
at Module.(sc_server)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fhello%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Fhello%2Froute.js&appDir=%2FUsers%2Favinashsethu%2FDesktop%2FDevelopment%2Fekali-lms%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2FUsers%2Favinashsethu%2FDesktop%2FDevelopment%2Fekali-lms&isDev=true&tsconfigPath=tsconfig.json&assetPrefix=!
at /Users/avinashsethu/Desktop/Development/ekali-lms/node_modules/next/dist/server/load-components.js:49:73
at async Object.loadComponentsImpl [as loadComponents] (/Users/avinashsethu/Desktop/Development/ekali-lms/node_modules/next/dist/server/load-components.js:49:26)
at async DevServer.findPageComponentsImpl (/Users/avinashsethu/Desktop/Development/ekali-lms/node_modules/next/dist/server/next-server.js:599:36) {
page: ‘/api/hello’
}
I need to connect the mongoDB to next js application then only i can able to build an application without this i cant able to create an application
3
Answers
Try to write in
next.config.js
file:I am currently facing the same issue, after updating the mongoose dependencies to 7.0.1. Downgrading to mongoose "6.10.3" helped me to resolve the issue for the time being. I hope, this can help you as well!
These are the things i’ve learned by spending time using mongoose 7 with latest nextjs version:
Make sure that mongoose is not imported inside client components ! (Which were creating Can’t resolve errors that you are facing)
And if you are using mongoose 7, I have to put serverComponentsExternalPackages option in next.config.js (even with the fix given here https://github.com/vercel/next.js/issues/42277)
I would also change your export by
my project https://github.com/mathieuguyot/adventures have mongoose 7 integrated with next 13.2.4, maybe it can help you in any way 🙂