I have the following situation in a NextJS app on Heroku.
In the root directory there are the app folder and a lib folder. The app folder contains a subfolder (components). The lib folder contains a file (mongoDB.tsx), which contents is:
import mongoose from "mongoose"
const uri = process.env.MDB_URI_AUDIO!,
connectMDB = async () => mongoose.connect(uri)
export default connectMDB
At this point I can run successfully this command:
% npm run build
From now on I want to add a new route to the app, and this is where troubles are going to show up.
In the app folder I add a subfolder (Channels_Pool) for the new route. And Channels_Pool contains a file (page.tsx), which contents is:
import ChannelList from '../components/ChannelList'
export default function NxtJSRoute() {
return (
<>
<ChannelList />
</>
)
}
And this is the contents of ChannelList (inside components):
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 */
Running now this command:
% npm run build
leads to a failure with this error:
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.
I have been able to check that this line is causing the problem:
const theResult = await channelsPool()
Can someone see what I am doing wrong and what the solution is ?