在 Next js 13.2 应用程序目录中的 api 目录中导入 Mongoose lib 会出现错误
在 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)
}
在 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;
错误显示
错误 - node_modules/mongoose/lib/types/objectid.js (21:31) @prototype 错误 - TypeError:无法读取未定义的属性(读取“prototype”) 在 eval (webpack-internal:///(sc_server)/./node_modules/mongoose/lib/types/objectid.js:15:36) 在 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) 在 webpack_require (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/webpack-runtime.js:33:43) 位于 eval (webpack-internal:///(sc_server)/./node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:7:18) 位于 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) 位于 webpack_require (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/webpack-runtime.js:33:43) 在 eval 处(webpack-internal:///(sc_server)/./node_modules/mongoose/lib/drivers/node-mongodb-native/index.js:4:22) 在 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) 在 webpack_require 处(/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/webpack-runtime.js:33:43) 在 eval 处(webpack-internal:///(sc_server)/./node_modules/mongoose/lib/index.js:4:100) 位于 Object.(sc_server)/./node_modules/mongoose/lib/index.js (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/app/api/hello/route.js:2057:1) 位于 webpack_require (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/webpack-runtime.js:33:43) 位于 eval (webpack-internal:///(sc_server)/./node_modules/mongoose/index.js:5:18) 位于 Object.(sc_server)/./node_modules/mongoose/index.js (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/app/api/hello/route.js:242:1) 位于 webpack_require (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/webpack-runtime.js:33:43) 位于 eval (webpack-internal:///(sc_server)/./models/UserModel.js:5:66) 位于 Module.(sc_server)/./models/UserModel.js (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/app/api/hello/route.js:154:1) 位于 webpack_require (/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/webpack-runtime.js:33:43) 在 eval 处(webpack-internal:///(sc_server)/./app/api/hello/route.js:6:75) 在 Module.(sc_server)/./app/api/hello/route.js 处(/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/app/api/hello/route.js:143:1) 在 webpack_require 处(/Users/avinashsethu/Desktop/Development/ekali-lms/.next/server/webpack-runtime.js:33:43) 在 eval 处(webpack 内部:///(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%2FDevelopm ent%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模块。(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%2FDevelopmen t%2Fekali-lms%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2FUsers%2Favinashsethu%2FDesktop%2FDevelopment%2Fekali-lms&isDev=true&tsconfigPath=tsconfig.json&assetPrefix=! 在 /Users/avinashsethu/Desktop/Development/ekali-lms/node_modules/next/dist/server/load-components.js:49:73 在异步 Object.loadComponentsImpl [作为 loadComponents] (/Users/avinashsethu/Desktop/Development/ekali-lms/node_modules/next/dist/server/load-components.js:49:26) 在异步 DevServer.findPageComponentsImpl (/Users/avinashsethu/Desktop/Development/ekali-lms/node_modules/next/dist/server/next-server.js:599:36) { 页面:'/api/hello'
我需要将 mongoDB 连接到下一个 js 应用程序,然后我才能构建应用程序,没有这个,我无法创建应用程序
这些是我通过使用 mongoose 7 和最新的 nextjs 版本学到的东西:
确保 mongoose 未在客户端组件中导入! (创建了无法解决您所面临的错误)
如果您使用的是 mongoose 7,我必须在 next.config.js 中放入 serverComponentsExternalPackages 选项(即使使用了此处给出的修复 https://github.com/vercel/next.js/issues/42277 )
/** @type {import("next").NextConfig} */
module.exports = {
experimental: { appDir: true, serverComponentsExternalPackages: ["mongoose"] },
webpack(config) {
config.experiments = { ...config.experiments, topLevelAwait: true };
return config;
}
};
我也会通过
const Product = mongoose.models && "Product" in mongoose.models ? mongoose.models. Product : mongoose.model("Product", PostSchema);
export default Product;
我的项目 https://github.com/mathieuguyot/adventures 已将 mongoose 7 与 next 13.2.4 集成,也许它可以为您提供帮助无论如何 :)
在将 mongoose 依赖项更新到 7.0.1 后,我目前面临同样的问题。降级到 mongoose“6.10.3”暂时帮助我解决了这个问题。我希望这也能帮助你!
尝试在
next.config.js
文件中写入:
experimental: {
appDir: true,
serverComponentsExternalPackages: ["mongoose"],
},