MongooseError:openUri() 的 uri 参数必须是字符串

MongooseError: The uri parameter to openUri() must be a string

提问人:Michel 提问时间:11/16/2023 更新时间:11/16/2023 访问量:30

问:

我在 Heroku 上的 NextJS 应用程序中遇到以下情况。

在根目录中有 app 文件夹和一个 lib 文件夹。应用文件夹包含一个子文件夹(组件)。 lib 文件夹包含一个文件 (mongoDB.tsx),其内容为:

import mongoose from "mongoose"

const uri = process.env.MDB_URI_AUDIO!,
            connectMDB = async () => mongoose.connect(uri)

export default connectMDB

此时,我可以成功运行此命令:

% npm run build

从现在开始,我想在应用程序中添加一条新路线,这就是麻烦会出现的地方。

在 app 文件夹中,我为新路由添加了一个子文件夹 (Channels_Pool)。 Channels_Pool包含一个文件(page.tsx),其内容为:

import ChannelList from '../components/ChannelList'

export default function NxtJSRoute() {

  return (
    <>
    <ChannelList />
    </>
  )
}

这是 ChannelList 的内容(在组件内部):

import channelsPool from "@/lib/channels";

export default async function ChannelList() {
  const theResult = await channelsPool() // This leads to an error !!

  return <div>
  {
    <div>Hello-ChannelList</div>
    // More useful code is to come here later, making use of theResult.
  }
  </div>
} /* End of ChannelList */

现在运行以下命令:

% npm 运行构建

导致失败,并出现以下错误:

MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.

我已经能够检查此行是否导致了问题:

const theResult = await channelsPool()

有人能看到我做错了什么以及解决方案是什么吗?

MongoDB 猫鼬 heroku-api

评论


答: 暂无答案