Why do I get the Must call setClient()
with an instance of MongoClient error ?
#11494
-
https://mongoosejs.com/docs/api/connection.html#connection_Connection-setClient
import mongoose from 'mongoose';
import { MongoClient } from 'mongodb';
async function run() {
try {
const uri = 'mongodb://localhost:27017';
// Create a new MongoClient
const client = new MongoClient(uri);
console.log(client);
const conn = mongoose.createConnection().setClient(client);
conn.getClient(); // MongoClient { ... }
conn.readyState; // 1, means 'CONNECTED'
} catch (error) {
console.log(error);
}
}
run(); This returns From what I understand is that I can use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 14 replies
-
The error message I get when running the above script is:
Which is fairly self explanatory, you need to do The error message you posted, though, makes me think that you have an incompatible version of the mongodb driver. Try running
|
Beta Was this translation helpful? Give feedback.
The error message I get when running the above script is:
Which is fairly self explanatory, you need to do
await client.connect()
before callingsetClient()
.The error message you posted, though, makes me think that you have an incompatible version of the mongodb driver. Try running
npm list | grep "mongo"
and paste the output here. The output should look something like: