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 :: lastindexof 
Javascript :: url decoding js 
Javascript :: jq get value without quotes 
Javascript :: Check if instance is array 
Javascript :: javascript swap array elements 
Javascript :: javascript parse date in current timezone 
Javascript :: javascript get object in object 
Javascript :: get index of first number in string javascript 
Javascript :: Creating with the custom hook in react 
Javascript :: generate qr code react 
Javascript :: defining schema mongoose 
Javascript :: jquery animate transform 
Javascript :: onclick increase counter javascript 
Javascript :: add active in nav 
Javascript :: Iterate Through the Keys of an Object 
Javascript :: how to get promise state in js 
Javascript :: how to check if string contains substring javascript 
Javascript :: how to add an event listener to a function javascript 
Javascript :: javascript escape character 
Javascript :: how to use javascript to hide content and show through link 
Javascript :: find string length javascript 
Javascript :: js array clear 
Javascript :: how to update state in react 
Javascript :: shadow react native generator 
Javascript :: @input in angular 
Javascript :: delete request from the script to html 
Javascript :: test each jest 
Javascript :: vuejs pass all events to child 
Javascript :: crdit card input format 
Javascript :: react extends component Increment data 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =