With node js i connect to mongo db.
I have user created in database and security enabled on mongo db conf file.
mongoose
.connect(
"mongodb://user:password@domain:27017/database?authSource=admin"
)
.then(() => {
console.log("connected to mongodb");
httpServer.listen(4001, () => {
console.log("API is listening on port 4001");
});
})
.catch((error) => {
console.error("Error connecting to MongoDB:", error);
});
The above throws: command find requires authentication. It fails to authenticate on mongo.
But if i do directly in the command line:
mongosh "mongodb://user:password@domain:27017/database?authSource=admin"
It connects without problem.
node js version 20.14.0
i tried everything i read about.
Lots of stuff without success.
New contributor
André Castro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.