Jess182
Jess1823y ago

Problem with Mongoose + auth

Hi i followed the mongoose example provided by denoland, but if my local database has authentication, mongoose not connect (it freeze on connect statemment) example: https://github.com/denoland/examples/tree/main/with-mongoose
import mongoose, { model, Schema } from 'npm:mongoose@^6.7';

const dinosaurSchema = new Schema({
name: { type: String, unique: true },
description: String,
createdAt: { type: Date, default: Date.now },
updatedAt: { type: Date, default: Date.now },
});

const Dinosaur = model('Dinosaur', dinosaurSchema);

// await mongoose.connect('mongodb://localhost:27017');

await mongoose.connect('mongodb://username:password@localhost:27017');

// Check to see connection status.
console.log(mongoose.connection.readyState);

console.log(await Dinosaur.find());
import mongoose, { model, Schema } from 'npm:mongoose@^6.7';

const dinosaurSchema = new Schema({
name: { type: String, unique: true },
description: String,
createdAt: { type: Date, default: Date.now },
updatedAt: { type: Date, default: Date.now },
});

const Dinosaur = model('Dinosaur', dinosaurSchema);

// await mongoose.connect('mongodb://localhost:27017');

await mongoose.connect('mongodb://username:password@localhost:27017');

// Check to see connection status.
console.log(mongoose.connection.readyState);

console.log(await Dinosaur.find());
Note: same URI works with node + mongoose
5 Replies
Andy (he/him)
Andy (he/him)3y ago
is the mongo uri provided to mongoose.connect correct?
Jess182
Jess182OP3y ago
right @spam.wizrad
Andy (he/him)
Andy (he/him)3y ago
what kind of authentication does your local mongodb use?
Jess182
Jess182OP3y ago
Default, i just set MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOTPASSWORD on my docker image as documentation says https://hub.docker.com//mongo if i try to connect to mongo atlas with SCRAM auth, give me another problem @spam.wizrad Uncaught MongoAPIError: Server record does not share hostname with parent URI
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View