I recently set up a next.js application using the mongodb template:
npx create-next-app --e with-mongodb my-app
Additionally, I included TypeScript in my project. Now, I am faced with the task of converting /lib/mongodb.js
to TypeScript.
Currently, the file looks like this with minimal type changes:
// lib/mongodb.ts
import { MongoClient } from 'mongodb'
const uri = process.env.MONGODB_URI
const options = {}
let client
let clientPromise: MongoClient
if (!process.env.MONGODB_URI) {
throw new Error('Please add your Mongo URI to .env.local')
}
if (process.env.NODE_ENV === 'development') {
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
However, I am encountering an error message: https://i.sstatic.net/8ssNc.png