Hey everyone!
I’m working on the third task of MongoDB and Mongoose, where I have to save a document.
This is my code, I’ll only include till the createAndSavePerson function:
require('dotenv').config();
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI);
const Schema = mongoose.Schema;
// create Schema
const personSchema = new Schema({
name: {type: String, required: true},
age: Number,
favoriteFoods: [String]
});
// create model
const Person = mongoose.model("Person", personSchema);
const createAndSavePerson = (done) => {
// create document
let nezumi = new Person({name: "Nezumi Shion", age: 100, favoriteFoods: ["eggs", "aubergines"]});
nezumi.save((err,data) => {
if(err) return console.log(err);
done(null, data);
});
This is my connection to mongoDB:
mongodb+srv://nezumiCodes:<password>@FreeCodeCamp.ut5mkho.mongodb.net/?retryWrites=true&w=majority
Also this is the error on the console:
GET
MongoError: (Unauthorized) not authorized on admin to execute command { insert: "people", documents: [[{favoriteFoods [eggs aubergines]} {_id ObjectID("62e7e842f9987215df47a82b")} {name Nezumi Shion} {age 100} {__v 0}]], ordered: true, writeConcern: { w: "majority" }, txnNumber: 2.000000, $clusterTime: { clusterTime: {1659365434 1}, signature: { hash: {0 [55 65 199 164 133 38 3 162 180 17 96 98 142 226 15 33 37 211 162 98]}, keyId: 7072364093310500864.000000 } }, lsid: { id: {4 [52 151 38 229 171 83 74 63 142 123 211 204 141 195 172 231]} } }
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 (node:events:527:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at TLSSocket.Readable.push (node:internal/streams/readable:228:10)
at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) {
ok: 0,
errmsg: '(Unauthorized) not authorized on admin to execute command { insert: "people", documents: [[{favoriteFoods [eggs aubergines]} {_id ObjectID("62e7e842f9987215df47a82b")} {name Nezumi Shion} {age 100} {__v 0}]], ordered: true, writeConcern: { w: "majority" }, txnNumber: 2.000000, $clusterTime: { clusterTime: {1659365434 1}, signature: { hash: {0 [55 65 199 164 133 38 3 162 180 17 96 98 142 226 15 33 37 211 162 98]}, keyId: 7072364093310500864.000000 } }, lsid: { id: {4 [52 151 38 229 171 83 74 63 142 123 211 204 141 195 172 231]} } }',
code: 8000,
codeName: 'AtlasError',
[Symbol(mongoErrorContextSymbol)]: {}
}
You can find the whole code here.
Could you please help me out, do you see any mistakes? It connects to the database but I can’t create a document. I have checked other topics on this task, but haven’t helped me
Link to the challenge: