Tell us what’s happening:
Getting error:
MongoError: (Unauthorized) not authorized on admin to execute command { insert: “people”, documents: [[{favoriteFoods [eggs fish fresh fruit]}
Your project link(s)
solution: fcc-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/113.0.0.0 Safari/537.36
Challenge: MongoDB and Mongoose - Create and Save a Record of a Model
Link to the challenge:
My code so far:
require('dotenv').config();
/** 1) Install & Set up mongoose */
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI);
// 2 Create a Model
const personSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
age: {
type: Number
},
favoriteFoods:
[String]
});
const Person = mongoose.model('Person', personSchema);
// 3 Create and Save a Record of a Model
var createAndSavePerson = function(done) {
var name = new Person(
{name: "William Step",
age: 84,
favoriteFoods: ["meat", "rice", "vegetables"]
});
name.save(function(err, data) {
if (err) return console.error(err);
done(null, data)
});
};