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

ive tried saving te record but i cant pass the test. i am connected to my mongodb cluster and i created a database and connected to it but whenever i try to save the record it doesnt save to the mongodb cluster. this is my code

Your code so far

require('dotenv').config();
const mongoose = require("mongoose")
mongoose.connect("mongodb+srv://ahmusa:System123@cluster0.k7nfcp0.mongodb.net/db1?retryWrites=true&w=majority", { useNewUrlParser: true, useUnifiedTopology: true });
const personSchema = new mongoose.Schema({
name : {type : String,
       required : true
       },
age :  Number,
favoriteFoods : [String]
})
let Person = mongoose.model("Person", personSchema);

const createAndSavePerson = (done) => {
  let ahmed =new Person({name : "ahmed", age : 20, favoriteFoods : ["food"]})
  ahmed.save((data,error)=>{
if(error){
  console.log(error)
}else{
  done(null,data)
}
    
  })

};

const createManyPeople = (arrayOfPeople, done) => {
  done(null /*, data*/);
};

const findPeopleByName = (personName, done) => {
  done(null /*, data*/);
};

const findOneByFood = (food, done) => {
  done(null /*, data*/);
};

const findPersonById = (personId, done) => {
  done(null /*, data*/);
};

const findEditThenSave = (personId, done) => {
  const foodToAdd = "hamburger";

  done(null /*, data*/);
};

const findAndUpdate = (personName, done) => {
  const ageToSet = 20;

  done(null /*, data*/);
};

const removeById = (personId, done) => {
  done(null /*, data*/);
};

const removeManyPeople = (done) => {
  const nameToRemove = "Mary";

  done(null /*, data*/);
};

const queryChain = (done) => {
  const foodToSearch = "burrito";

  done(null /*, data*/);
};

/** **Well Done !!**
/* You completed these challenges, let's go celebrate !
 */

//----- **DO NOT EDIT BELOW THIS LINE** ----------------------------------

exports.PersonModel = Person;
exports.createAndSavePerson = createAndSavePerson;
exports.findPeopleByName = findPeopleByName;
exports.findOneByFood = findOneByFood;
exports.findPersonById = findPersonById;
exports.findEditThenSave = findEditThenSave;
exports.findAndUpdate = findAndUpdate;
exports.createManyPeople = createManyPeople;
exports.removeById = removeById;
exports.removeManyPeople = removeManyPeople;
exports.queryChain = queryChain;

Your browser information:

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

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

Link to the challenge:

also getting this error from the npm console “MongoError: user is not allowed to do action [insert] on [oxxo.people]”
https://replit.com/@ahmusa118/boilerplate-mongomongoose-3#myApp.js

figured it out. had to change user roles to readWrite in mongodb

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