从今天开始,当我尝试在Next.js应用程序中使用mongoose连接到mongodb时,我得到了下面的错误。有人知道该怎么做吗?
Uncaught(in promise)TypeError:mongoose__WEBPACK_IMPORTED_MODULE_0___default(...).connect不是mongoDBConnection中的函数
app/new/FileUpload.tsx:
import mongoDBConnection from "../lib/mongoDb";
useEffect(()=> mongoDBConnection()),[]}
app/lib/mongoDB.ts:
import mongoose from "mongoose";
const mongoDBConnection = async () =>
await mongoose.connect(process.env.MONGO_URL as string);
export default mongoDBConnection;
next.config.js:
/** @type {import('next').NextConfig} */
const nextConfig = {
webpack: (config) => {
config.experiments = { ...config.experiments, topLevelAwait: true };
return config;
},
reactStrictMode: true,
experimental: {
serverActions: true,
appDir: true,
serverComponentsExternalPackages: ["mongoose"],
},
images: {
domains: ["fakestoreapi.com", "firebasestorage.googleapis.com"],
},
};
module.exports = nextConfig;
1条答案
按热度按时间vd2z7a6w1#
解决方案是我必须在服务器端进行连接调用。所以在接下来它必须在app/API/文件夹中。