rsnext/examples/with-mongodb-mongoose
chornos13 a03cdc6d35
docs(examples): fix error connection handling (#40633)
When there's an error while connect to mongodb (timeout for example), it will be cached and need to restart next.js, by handling the error correctly (clear cached promise) it will try to reconnect to mongodb if the function is called again

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-09-19 21:26:24 +00:00
..
components Enable @typescript-eslint/no-use-before-define for examples dir (#39469) 2022-08-10 16:30:36 +00:00
css Use Font Optimization in examples (#39977) 2022-08-26 18:27:59 +00:00
lib docs(examples): fix error connection handling (#40633) 2022-09-19 21:26:24 +00:00
models fix(examples): maxlength was inconsistent (#36840) 2022-05-13 14:32:08 +00:00
pages Use Font Optimization in examples (#39977) 2022-08-26 18:27:59 +00:00
public [ EXAMPLES ] Added with-mongodb example (#13028) 2020-05-30 12:15:00 -05:00
.env.local.example [ EXAMPLES ] Added with-mongodb example (#13028) 2020-05-30 12:15:00 -05:00
.gitignore Update default gitignore templates (#39051) 2022-07-26 20:08:40 -05:00
package.json Update mongoose and swr and remove no longer needed connection options (#29934) 2021-10-16 06:33:06 +00:00
README.md docs(examples): improve DX while copying command to create new project (#38410) 2022-07-26 21:57:48 -05:00

MongoDB and Mongoose with Next.js

This example shows how you can use a MongoDB database to support your Next.js application.

Pet is an application that allows users to add their pets' information (e.g., name, owner's name, diet, age, dislikes, likes, and photo). They can also delete it or edit it anytime.

Deploy your own

Once you have access to the environment variables you'll need, deploy the example using Vercel:

Deploy with Vercel

How to use

Execute create-next-app with npm, Yarn, or pnpm to bootstrap the example:

npx create-next-app --example with-mongodb-mongoose with-mongodb-mongoose-app
yarn create next-app --example with-mongodb-mongoose with-mongodb-mongoose-app
pnpm create next-app --example with-mongodb-mongoose with-mongodb-mongoose-app

Configuration

Step 1. Get the connection string of your MongoDB server

In the case of MongoDB Atlas, it should be a string like this:

mongodb+srv://<username>:<password>@my-project-abc123.mongodb.net/test?retryWrites=true&w=majority

For more details, follow this MongoDB Guide on how to connect to MongoDB.

Step 2. Set up environment variables

Copy the .env.local.example file in this directory to .env.local (which will be ignored by Git):

cp .env.local.example .env.local

Then set each variable on .env.local:

  • MONGODB_URI should be the MongoDB connection string you got from step 1.

Step 3. Run Next.js in development mode

npm install
npm run dev

# or

yarn install
yarn dev

Your app should be up and running on http://localhost:3000! If it doesn't work, post on GitHub discussions.

Deploy on Vercel

You can deploy this app to the cloud with Vercel (Documentation).

Deploy Your Local Project

To deploy your local project to Vercel, push it to GitHub/GitLab/Bitbucket and import to Vercel.

Important: When you import your project on Vercel, make sure to click on Environment Variables and set them to match your .env.local file.

Deploy from Our Template

Alternatively, you can deploy using our template by clicking on the Deploy button below.

Deploy with Vercel