Take look at this code and please tell me why it gives an error each time I try to save this record in the database.
CODE:
const mongoose = require(‘mongoose’);
const validator = require(‘validator’);
mongoose.connect(process.env.MONGO_URI);
const personSchema = new mongoose.Schema({
name : { type : String, required: true },
age : Number,
favoriteFoods : [String]
});
const Person = mongoose.model(‘Person’, personSchema);
const createAndSavePerson = (done) => {
let Ahmad = new Person({name : “Ahmad”, age : 19, favoriteFoods :
[‘mango’, ‘biryani’, “BBQ”]});
Ahmad.save(function(err, data) {
if (err) return console.error(err);
done(err, data);
});
};
solution: https://replit.com/@pin-z/boilerplate-mongomongoose
ERROR:
errmsg: ‘(Unauthorized) not authorized on admin to execute command { insert: “people”, documents: [[{favoriteFoods [mango biryani BBQ]} {_id ObjectID(“62eae0788adf2716a5825bba”)} {name Ahmad} {age 19} {__v 0}]], ordered: true, writeConcern: { w: “majority” }, txnNumber: 1.000000, $clusterTime: { clusterTime: {1659560050 5}, signature: { hash: {0 [11 153 173 197 189 49 120 83 36 149 150 104 168 6 3 66 12 75 18 94]}, keyId: 7082471950949285888.000000 } }, lsid: { id: {4 [153 61 27 229 22 210 76 199 158 152 98 63 1 191 243 111]} } }’,
code: 8000,
codeName: ‘AtlasError’,
[Symbol(mongoErrorContextSymbol)]: {}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.77
Challenge: MongoDB and Mongoose - Create and Save a Record of a Model
Link to the challenge: