Comments (12)
Hey @Anonymous7code I'm currently getting the same issue with connecting to Mongo, I added the config and it still doesn't work.
Could you also show what part of the code you've changed?
from mongoose.
What does your Webpack config look like? Are you using Next.js? We'll need a more complete repro to figure this out, because this is a Webpack error and your code looks like it is from Next.js' samples repo.
from mongoose.
I'm using NextJS 14 trying to connect to MongoDB via mongoose below is the connection code
import mongoose from "mongoose";
const MONGODB_URI = process.env.NEXT_PUBLIC_MONGODB_URI;
if (!MONGODB_URI) {
throw new Error(
"Please define the MONGODB_URI environment variable inside .env"
);
}
let cached = (global as any).mongoose || { conn: null, promise: null };
export const connectToDatabase = async () => {
if (cached.conn) return cached.conn;
if (!cached.promise) {
const opts = {
bufferCommands: false,
dbName: "festifusion",
};
cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
return mongoose;
});
}
cached.conn = await cached.promise;
return cached.conn;
};
and the next.config.js
file looks like it compiles well but the issue is still there
/** @type {import('next').NextConfig} */
const nextConfig = {
webpack: (config) => {
return config;
},
images: {
domains: ["utfs.io"],
remotePatterns: [
{
protocol: "https",
hostname: "utfs.io",
port: "",
},
],
},
};
module.exports = nextConfig;
if I use
webpack: (config) => {
config.experiments = {
topLevelAwait: true
};
return config;
},
then it takes indefinite time to compile the code. I also took a reference from Mongoose Documentation on using with NextJs but still the issue is there I've attached log file for reference purpose. Let me know if you require more details.
localhost-1705844188011.log
from mongoose.
Did you add the following to your Next.js config?
const nextConfig = {
experimental: {
esmExternals: "loose", // <-- add this
serverComponentsExternalPackages: ["mongoose"] // <-- and this
}
// ...
from mongoose.
Yes by adding this also the issue is not solved the whole connection.tsx
code has been mentioned above and by adding toplevelawait to true
in webpack config
it takes infinite amount of time to compile.
from mongoose.
Any update here? I am also stuck with same issue. I have added experimental
tags in next config, moved the connect statement inside a function. Nothing works.
"mongoose": "^7.4.2",
"next": "14.1.0",
Help would be great!
from mongoose.
We're unable to repro, this GitHub repo demonstrates that Mongoose works fine with Next.js 14. Please fork and modify this repo to demonstrate the issue you're seeing.
from mongoose.
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days
from mongoose.
in my case, because mongoose does not currently support Next Edge Runtime, so just remove line export const runtime = "edge"
and everything work well
from mongoose.
I now works by adding
const nextConfig = {
webpack: (config) => {
return config;
},
experimental: {
esmExternals: "loose", // <-- add this
serverComponentsExternalPackages: ["mongoose"]
}
}
just cleared the cache and deleted package-lock.json and node_modules
not with the same code it works!! Thanks for help :)
from mongoose.
experimental: {
esmExternals: "loose", // <-- add this
serverComponentsExternalPackages: ["mongoose"] // <-- and this
}
you don't even have to do above, it is totally fine with mongoose and Next 14 , this error is happening just because they are forgetting to mark their server action component with "use server" on top. JUST make sure to write "use server" for server actions that you created, thats it.
from mongoose.
just mark "use server" in all your server actions , that's all you need to do.
from mongoose.
Related Issues (20)
- Return the deleted document after a deleteOne operation HOT 2
- Describe `deleteOne` or `remove` method of Document in the doc HOT 3
- Merging of discrimintor schema "_indexes" duplicates when both 'from' and 'to' are schema instances HOT 1
- MongoServerError: $natural sort cannot be set to a value other than -1 or 1 when using $natural with another field in sorting HOT 3
- Document.toObject() does not apply to subdocuments HOT 1
- Pre validate: is not triggert on all sub documents when set via _.set() with a nested path HOT 1
- Using mongoose and got error when adding new data: Product validation failed HOT 1
- Discriminators: Return derived model when issuing Find on base model HOT 1
- When updating elements of primitive array using positional filtered operator, update value is incorrectly cast to array
- empty objects are being saved as null HOT 1
- Autoindex is recreating existing indexes HOT 2
- StrictPopulateError: Cannot populate path `totalLinksList` because it is not in your schema. Set the `strictPopulate` option to false to override. HOT 1
- Breaking types of `toObject` on SubDocument or ArraySubdocument HOT 1
- Make autoIndex syncIndex instead of createIndex
- Using Promise.all inside transactions errors with "Cannot pin multiple connections to the same session" HOT 6
- new Types.ObjectId is deprecated how do i convert a string _id to an ObjectID now? HOT 1
- Filter is mutated in place by Query modifiers
- command events are not fired after setting monitorCommands: true HOT 2
- log : import_mongoose.default.connect is not a function
- Type is incorrect for Query.distinct when field is a nested field HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoose.