Unable to use `mongodb` client

Hey there, I am having an issue using MongoDB with Better Auth. I have correctly initialized a mongodb client it seems, but Better Auth seems to be having an issue with it. If you have any pointers on how I might begin to debug this, I would greatly appreciate it. Error following authClient.signUp(). I can send the full error if needed by I am limited by Discord's post length here. Here the database is connected successfully.
✓ Compiled /api/auth/[...all] in 919ms

ERROR TypeError e.collection is not a function
Better Auth
at Object.findOne (.next/server/chunks/node_modules_better-auth_dist_c1259f._.js:4993:104)
at Object.findUserByEmail (.next/server/chunks/node_modules_better-auth_dist_c1259f._.js:3693:29)
...
POST /api/auth/sign-up/email?currentURL=http%3A%2F%2Flocalhost%3A3000%2F 500 in 1829ms
Pinged your deployment. You successfully connected to MongoDB!
✓ Compiled /api/auth/[...all] in 919ms

ERROR TypeError e.collection is not a function
Better Auth
at Object.findOne (.next/server/chunks/node_modules_better-auth_dist_c1259f._.js:4993:104)
at Object.findUserByEmail (.next/server/chunks/node_modules_better-auth_dist_c1259f._.js:3693:29)
...
POST /api/auth/sign-up/email?currentURL=http%3A%2F%2Flocalhost%3A3000%2F 500 in 1829ms
Pinged your deployment. You successfully connected to MongoDB!
/lib/auth/server.ts
import { betterAuth } from "better-auth";
import { mongodbAdapter } from "better-auth/adapters/mongodb";
import { client } from "@/lib/db/mongodb";

export const auth = betterAuth({
database: mongodbAdapter(client),
emailAndPassword: {
enabled: true,
},
});
import { betterAuth } from "better-auth";
import { mongodbAdapter } from "better-auth/adapters/mongodb";
import { client } from "@/lib/db/mongodb";

export const auth = betterAuth({
database: mongodbAdapter(client),
emailAndPassword: {
enabled: true,
},
});
/lib/db/mongodb.ts
const { MongoClient, ServerApiVersion } = require("mongodb");

const uri = process.env.MONGODB_URI as string;

export const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
});
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
await client.connect();
// Send a ping to confirm a successful connection
await client.db("admin").command({ ping: 1 });
console.log(
"Pinged your deployment. You successfully connected to MongoDB!"
);
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);
const { MongoClient, ServerApiVersion } = require("mongodb");

const uri = process.env.MONGODB_URI as string;

export const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
});
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
await client.connect();
// Send a ping to confirm a successful connection
await client.db("admin").command({ ping: 1 });
console.log(
"Pinged your deployment. You successfully connected to MongoDB!"
);
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);
2 Replies
bekacru
bekacru5mo ago
hey you should pass the collection instead of the client like client.db("my-collection") should be passed to the adapter
Json 👺
Json 👺OP5mo ago
Great I got it working. Thanks

Did you find this page helpful?