---
title: "Discussion on \"🗃️ How to use Mongoose with Next.js for MongoDB?\" | Hashnode"
description: "Discussion on \"🗃️ How to use Mongoose with Next.js for MongoDB?\". Explained with a simple project"
url: "https://hashnode.com/posts/how-to-use-mongoose-with-nextjs-for-mongodb/625bd39852fb7bd53380fbd2"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [🗃️ How to use Mongoose with Next.js for MongoDB?](https://blog.usmans.me/how-to-use-mongoose-with-nextjs-for-mongodb?utm_source=hashnode&utm_medium=feed)

Explained with a simple project

Apr 17, 2022 · 6 min read · blog.usmans.me  
Updated Jun 7, 2022  
Read the article

-   [#mongodb](https://hashnode.com/tag/mongodb)
-   [#mongoose](https://hashnode.com/tag/mongoose)
-   [#nextjs](https://hashnode.com/tag/nextjs)
-   [#databases](https://hashnode.com/tag/databases)
-   [#reactjs](https://hashnode.com/tag/reactjs)

## Written by

[Usman Sabuwala](https://hashnode.com/@usmanwrites)

@usmanwrites · India

  
[View profile](https://hashnode.com/@usmanwrites)

Learning web development, running a YouTube channel

I am a full stack developer, YouTuber and blogger!

## Responses 4

Markdown supported · ⌘↵ to post

[Suen](https://hashnode.com/@suen)

[Apr 15, 2023](https://hashnode.com/posts/how-to-use-mongoose-with-nextjs-for-mongodb/625bd39852fb7bd53380fbd2/comment/643b1cec97c2a8c1638068ba)

Thank you.

[Next Dev](https://hashnode.com/@nextdev)

[Dec 22, 2022](https://hashnode.com/posts/how-to-use-mongoose-with-nextjs-for-mongodb/625bd39852fb7bd53380fbd2/comment/63a3ba6df67b03c0a9633c5c)

Won't it take time for the server to first connect to the Mongodb server and then fetch data.

Is there any way by which we are connected to the Mongodb server forever and when request will be sent to it. It just has to fetch the data and return to the nextjs server function.

[Usman Sabuwala](https://hashnode.com/@usmanwrites)

[Dec 22, 2022](https://hashnode.com/posts/how-to-use-mongoose-with-nextjs-for-mongodb/625bd39852fb7bd53380fbd2/comment/63a3ba6df67b03c0a9633c5c#reply-63a3bc84f7fa1fc0ce9eb6c5)

That's actually not possible due to the behaviour of API Routes in Next.js.

Next.js has API Routes and those are AWS Lambda functions. It's not a fully fledged backend and that's the reason we have to connect to Mongo everytime.

Watch this video, it explains how backend works in Next.js [youtu.be/Rrz2q5uCHdE](https://youtu.be/Rrz2q5uCHdE)

[nandan vasista](https://hashnode.com/@nandan910)

[Nov 1, 2022](https://hashnode.com/posts/how-to-use-mongoose-with-nextjs-for-mongodb/625bd39852fb7bd53380fbd2/comment/636151488dbe21712c7dc036)

This worked for me

\`\`\`import { Schema, model, models } from "mongoose";

const UserSchema = new Schema({ displayName: { type: String, required: true }, email: { type: String, required: true, unique: true }, phone: { type: String, unique: true }, password: { type: String }, pro: { type: Boolean, default: false }, author: { type: Boolean, default: false }, photoURL: { type: String }, }); console.log(model); // undefined console.log(models.UserModel); //this solved const User = models.UserModel;

export default User;

\`\`\`

[nandan vasista](https://hashnode.com/@nandan910)

[Nov 1, 2022](https://hashnode.com/posts/how-to-use-mongoose-with-nextjs-for-mongodb/625bd39852fb7bd53380fbd2/comment/636150148dbe21712c7dc004)

Server Error TypeError: (0 , mongoose**WEBPACK\_IMPORTED\_MODULE\_0**.model) is not a function

This error happened while generating the page. Any console logs will be displayed in the terminal window. --error above-- --code below--

import { Schema, model, models } from "mongoose";

const UserSchema = new Schema({ displayName: { type: String, required: true }, email: { type: String, required: true, unique: true }, phone: { type: String, unique: true }, password: { type: String }, pro: { type: Boolean, default: false }, author: { type: Boolean, default: false }, photoURL: { type: String }, }); const User = models.User || model("User", UserSchema);

export default User;
