Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mongodb mongoose with next js connection

import mongoose from 'mongoose'

const MONGODB_URI = process.env.MONGODB_URI

if (!MONGODB_URI) {
  throw new Error(
    'Please define the MONGODB_URI environment variable inside .env.local'
  )
}

/**
 * Global is used here to maintain a cached connection across hot reloads
 * in development. This prevents connections growing exponentially
 * during API Route usage.
 */
let cached = global.mongoose

if (!cached) {
  cached = global.mongoose = { conn: null, promise: null }
}

async function dbConnect() {
  if (cached.conn) {
    return cached.conn
  }

  if (!cached.promise) {
    const opts = {
      bufferCommands: false,
    }

    cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
      return mongoose
    })
  }
  cached.conn = await cached.promise
  return cached.conn
}

export default dbConnect
Comment

mongodb mongoose with next js connection

import mongoose from 'mongoose'

const MONGODB_URI = process.env.MONGODB_URI

if (!MONGODB_URI) {
  throw new Error(
    'Please define the MONGODB_URI environment variable inside .env.local'
  )
}

/**
 * Global is used here to maintain a cached connection across hot reloads
 * in development. This prevents connections growing exponentially
 * during API Route usage.
 */
let cached = global.mongoose

if (!cached) {
  cached = global.mongoose = { conn: null, promise: null }
}

async function dbConnect() {
  if (cached.conn) {
    return cached.conn
  }

  if (!cached.promise) {
    const opts = {
      bufferCommands: false,
    }

    cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
      return mongoose
    })
  }
  cached.conn = await cached.promise
  return cached.conn
}

export default dbConnect
Comment

PREVIOUS NEXT
Code Example
Javascript :: access json object in javascript loop 
Javascript :: Uncaught (in promise) DOMException: Failed to load because no supported source was found. 
Javascript :: currentTime(); javascript 
Javascript :: react router link with params 
Javascript :: change image onclick js 
Javascript :: anonymous function javascript 
Javascript :: funciones invocan a funciones javascript 
Javascript :: trigger keyboard event javascript 
Javascript :: react media recoder 
Javascript :: command to start a new react app using vite 
Javascript :: hackerearth javascript solutions 
Javascript :: Not Found The requested URL was not found on this server angular routing when going back to site from ecternal source 
Javascript :: react native get location 
Javascript :: today date selected in datepicker 
Javascript :: regex scan youtube video id 
Javascript :: javascript include a js file from another 
Javascript :: react chat sheet 
Javascript :: curl send json as variable 
Javascript :: js sleep 1 sec 
Javascript :: remove substring from string liquid shopify 
Javascript :: updatable time js 
Javascript :: mongoose find get nested prop only 
Javascript :: linear gradient react native 
Javascript :: date without seconds react 
Javascript :: reactjs navbar component 
Javascript :: template literals in javascript 
Javascript :: npm hook form 
Javascript :: svg css viewbox 
Javascript :: using express js response render parameter in ejs script tag as a variable in node js 
Javascript :: setProps jest 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =