Open JahanzaibTayyab opened 1 year ago
Hello,
For the problem you Have, I manage to get things working replacing your line with:
export const Post = mongoose.models && "Post" in mongoose.models ? mongoose.models.Post : mongoose.model("Post", PostSchema);
Also, make sure that mongoose is not imported inside client components !
And if you are using mongoose 7, I have to put serverComponentsExternalPackages option in next.config.js (even with the fix given here https://github.com/vercel/next.js/issues/42277)
/** @type {import("next").NextConfig} */
module.exports = {
experimental: { appDir: true, serverComponentsExternalPackages: ["mongoose"] },
webpack(config) {
config.experiments = { ...config.experiments, topLevelAwait: true };
return config;
}
};
my project https://github.com/mathieuguyot/adventures have mongoose 7 integrated with next 13.2.4, maybe it can help you in any way :)
Verify canary release
Provide environment information
Operating System: Platform: darwin Arch: x64 Version: Darwin Kernel Version 21.6.0: Mon Dec 19 20:44:01 PST 2022; root:xnu-8020.240.18~2/RELEASE_X86_64 Binaries: Node: 18.12.1 npm: 8.19.2 Yarn: 1.22.19 pnpm: N/A Relevant packages: next: 13.1.6 eslint-config-next: 13.1.6 react: 18.2.0 react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)
App directory (appDir: true)
Link to the code that reproduces this issue
https://github.com/JahanzaibTayyab/portfolio_projects/tree/main/ai-image-generation
To Reproduce
Whenever i request for data then end point throw an error.
Describe the Bug
Mongodb connection
Post.tsx
Post Data API
When i run my next application and call api/post then this error occurs
somehow i fixed this issue with some changes in Post.tsx
but again when i hit api/post every time this issues occurs
Expected Behavior
application should work this setup
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response