typescript 尝试连接到mongoose时出错

ss2ws0br  于 2023-05-19  发布在  TypeScript
关注(0)|答案(1)|浏览(227)

从今天开始,当我尝试在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;
vd2z7a6w

vd2z7a6w1#

解决方案是我必须在服务器端进行连接调用。所以在接下来它必须在app/API/文件夹中。

相关问题