Error while performing challenge: (Unauthorized) not authorized on admin to execute command

Tell us what’s happening:
I get the error below when trying to submit my solution to the challenge mentioned below. Please help! Thank you!

Here is the code:

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

let Person = mongoose.model('Person', personSchema);

const createAndSavePerson = (done) => {
  const p = new Person({name: 'Test', age: 23, favoriteFoods: ['Chicken', 'Cottage Cheese']});
  p.save(function(err, data) {
    if (err) console.log(err);
    done(null, data);
  });
};

Here is the full error:

MongoError: (Unauthorized) not authorized on admin to execute command { insert: "people", documents: [[{favoriteFoods [Chicken Cottage Cheese]} {_id ObjectID("628acab9a823d30d0d776ff2")} {name Test} {age 23} {__v 0}]], ordered: true, writeConcern: { w: "majority" }, txnNumber: 1.000000, $clusterTime: { clusterTime: {1653263026 20}, signature: { hash: {0 [237 98 235 194 149 108 174 92 190 220 50 179 60 59 111 113 158 233 35 4]}, keyId: 7065361548566331392.000000 } }, lsid: { id: {4 [55 181 14 94 194 85 78 173 186 128 170 117 145 159 221 189]} } }
    at /home/runner/boilerplate-mongomongoose/node_modules/mongodb-core/lib/connection/pool.js:581:63
    at authenticateStragglers (/home/runner/boilerplate-mongomongoose/node_modules/mongodb-core/lib/connection/pool.js:504:16)
    at Connection.messageHandler (/home/runner/boilerplate-mongomongoose/node_modules/mongodb-core/lib/connection/pool.js:540:5)
    at emitMessageHandler (/home/runner/boilerplate-mongomongoose/node_modules/mongodb-core/lib/connection/connection.js:310:10)
    at TLSSocket.<anonymous> (/home/runner/boilerplate-mongomongoose/node_modules/mongodb-core/lib/connection/connection.js:453:17)
    at TLSSocket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at TLSSocket.Readable.push (_stream_readable.js:213:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23) {
  ok: 0,
  errmsg: '(Unauthorized) not authorized on admin to execute command { insert: "people", documents: [[{favoriteFoods [Chicken Cottage Cheese]} {_id ObjectID("628acab9a823d30d0d776ff2")} {name Test} {age 23} {__v 0}]], ordered: true, writeConcern: { w: "majority" }, txnNumber: 1.000000, $clusterTime: { clusterTime: {1653263026 20}, signature: { hash: {0 [237 98 235 194 149 108 174 92 190 220 50 179 60 59 111 113 158 233 35 4]}, keyId: 7065361548566331392.000000 } }, lsid: { id: {4 [55 181 14 94 194 85 78 173 186 128 170 117 145 159 221 189]} } }',
  code: 8000,
  codeName: 'AtlasError',
  [Symbol(mongoErrorContextSymbol)]: {}
}

Your project link(s)

solution: boilerplate-mongomongoose - Replit

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36

Challenge: Create and Save a Record of a Model

Link to the challenge:

1 Like

Hi i have the same issue , you could resolve it ?

1 Like

Did anyone find a solution to this?

Hi, I have the same issue as explained above. Can anyone help on this please?

Thanks

The answer provided by Trey Cooper here worked out for me: Mongodb Atlas: not authorized on admin to execute command - Stack Overflow

1 Like