MongoDB and Mongoose - Create and Save a Record of a Model

Tell us what’s happening:
Describe your issue in detail here.
Keep getting a timed out error as well as the following Mongo Error / Atlas Error. Have no idea what’s happening?

MongoError: (Unauthorized) not authorized on admin to execute command { insert: “people”, documents: [[{favoriteFoods [beef chicken pizza]} {_id ObjectID(“64af545dac88db02b413660c”)} {name Pete Rose} {age 82} {__v 0}]], ordered: true, lsid: { id: {4 [17 169 67 78 17 231 67 196 177 78 224 155 5 95 166 24]} }, txnNumber: 4.000000, $clusterTime: { clusterTime: {1689211990 7}, signature: { hash: {0 [222 203 29 121 38 50 157 107 169 201 74 17 33 31 150 157 96 73 134 112]}, keyId: 7244963813731074048.000000 } }, $db: “admin” }
at Connection. (/home/runner/boilerplate-mongomongoose/node_modules/mongodb/lib/core/connection/pool.js:453:61)
at Connection.emit (node:events:513:28)
at processMessage (/home/runner/boilerplate-mongomongoose/node_modules/mongodb/lib/core/connection/connection.js:456:10)
at TLSSocket. (/home/runner/boilerplate-mongomongoose/node_modules/mongodb/lib/core/connection/connection.js:625:15)
at TLSSocket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10)
at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) {
ok: 0,
code: 8000,
codeName: ‘AtlasError’

Here is the code I’m trying:

mongoose.connect(process.env.MONGO_URI);
const mySecret = process.env[‘MONGO_URI’]

const personSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
age: {
type: Number
},
favoriteFoods: {
type: [String]
}
});

let Person = mongoose.model(‘Person’, personSchema);

const createAndSavePerson = (done) => {
let peteRose = new Person({name: “Pete Rose”, age: 82, favoriteFoods: [“beef”, “chicken”, “pizza”]});

peteRose.save(function (err, data) {
if (err) {
console.log(err);
} else {
done(null, data);
}
})
};

Thanks in advance for any help that can be provided.

Your project link(s)

solution: boilerplate-mongomongoose - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: MongoDB and Mongoose - Create and Save a Record of a Model

Link to the challenge:

it looks like you might have a problem with your mongoURI , like the password is incorrect, username or even the user you’ve created don’t have the authorization to to modify delete or add new collections, try checking all of these

mongoose.connect(process.env.MONGO_URI);                   
const mySecret = process.env['MONGO_URI']

You have defined a constant here but not used it, which is fine but makes it redundant.

One problem though is that you’re missing configuration options in your connect command, so your MONGO_URI won’t be correctly parsed.

Did you follow the instructions in the initial setup step here?

Also, the tutorial linked in that description?

If you’ve followed all of these instructions correctly, you should have your MONGO_URI set up correctly and stored in your Secrets tab and your connect command should be correctly configured.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.