Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to connect mongodb with next js

// mongodb.js

import { MongoClient } from 'mongodb'

const uri = process.env.MONGODB_URI
const options = {
  useUnifiedTopology: true,
  useNewUrlParser: true,
}

let client
let clientPromise

if (!process.env.MONGODB_URI) {
  throw new Error('Add Mongo URI to .env.local')
}

if (process.env.NODE_ENV === 'development') {
Replacement).
  if (!global._mongoClientPromise) {
    client = new MongoClient(uri, options)
    global._mongoClientPromise = client.connect()
  }
  clientPromise = global._mongoClientPromise
} else {
  client = new MongoClient(uri, options)
  clientPromise = client.connect()
}

export default clientPromise
Comment

Nextjs mongodb connection setup

// mongodb.js

import { MongoClient } from 'mongodb'

const uri = process.env.MONGODB_URI
const options = {
  useUnifiedTopology: true,
  useNewUrlParser: true,
}

let client
let clientPromise

if (!process.env.MONGODB_URI) {
  throw new Error('Please add your Mongo URI to .env.local')
}

if (process.env.NODE_ENV === 'development') {
  // In development mode, use a global variable so that the value
  // is preserved across module reloads caused by HMR (Hot Module Replacement).
  if (!global._mongoClientPromise) {
    client = new MongoClient(uri, options)
    global._mongoClientPromise = client.connect()
  }
  clientPromise = global._mongoClientPromise
} else {
  // In production mode, it's best to not use a global variable.
  client = new MongoClient(uri, options)
  clientPromise = client.connect()
}

// Export a module-scoped MongoClient promise. By doing this in a
// separate module, the client can be shared across functions.
export default clientPromise
Comment

PREVIOUS NEXT
Code Example
Javascript :: react hotjar 
Javascript :: vue js data bind 
Javascript :: vue 3 props 
Javascript :: timestamp discord.js 
Javascript :: Connect node.js with react.js 
Javascript :: node is not recognized as internal command 
Javascript :: like php date("Y-m-d",$value) in javascript 
Javascript :: reactjs facebook login popup trigger on load page 
Javascript :: get buildspec.yml file for react app 
Javascript :: Using a decrementing For Loop to Reverse an Array 
Javascript :: Get Country from the international phone number 
Javascript :: adding all elements in an array javascript 
Javascript :: find consecutive numbers in an array javascript 
Javascript :: date difference without weekends using moment js 
Javascript :: javascript copy object except one property 
Javascript :: how to define cardTitle background image in mdl in reactjs 
Javascript :: lodash omitby 
Javascript :: monaco editor cdn 
Javascript :: javascript convert string to bool 
Javascript :: get date in milliseconds javascript 
Javascript :: fs renameSync 
Javascript :: Error: [Home] is not a <Route component. All component children of <Routes must be a <Route or <React.Fragment 
Javascript :: graphql yoga access http headers 
Javascript :: to do list app react code 
Javascript :: Google App Script getSheetByName 
Javascript :: javascript access pushed element 
Javascript :: adding cors parameters to extjs ajax 
Javascript :: expressjs param 
Javascript :: install react hotjar 
Javascript :: Repeat a String Repeat a String-Javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =