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

Tell us what’s happening:
Describe your issue in detail here.
What is the reason why this is not working?

require('dotenv').config();
let mongoose=require('mongoose');



const createAndSavePerson = (done) => {
  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*/);
};
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });




var Schema = mongoose.Schema;

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


let aaron =  new Person({
    name: "Aaron",
    age: 29,
    favoriteFoods:["Sushi","Ice Cream"]
   
});


  function createAndSavePerson(){
    let alan = new Person({
          name: "Alan",
          age: "20",
          favoriteFoods:["Water","Salads"]
         
    }); 
      alan.save(function(error,data){
           if(error){
              console.log(error);
           }else{
             done(null,data);
           }
      });
  }  

Your project link(s)

solution: boilerplate-mongomongoose (2) - Node.js Repl - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; 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:

I believe you are expected to edit this function, not add a new function.

1 Like

Yes, exactly. The console tells me: SyntaxError: Identifier ‘createAndSavePerson’ has already been declared and I do not understand the reason why.

Just as @nhcarrigan stated above, the function has already been defined and you attempted to define it again at the bottom of the file. Move your code to the existing function that has already been declared for you.

2 Likes

It does not now work yet.

const createAndSavePerson = (done) => {
  let alan = new Person({
          name: "Alan",
          age: "20",
          favoriteFoods:["Water","Salads"]
         
    }); 
   
    alan.save(function(error,data){
           if(error){
              return done(error);
           }else{
             return done(null,data);
           }
      });
   
  };

You should pass this challenge with the code you currently have in your replit. I tested it with my own MONGO_URI and passed. If you are still not passing, do you see any errors showing in the Node console of the replit when you try to submit your project url?

1 Like

I will check this: “Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you’re trying to access the database from an IP that isn’t whitelisted. Make sure your current IP address is on your Atlas cluster’s IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/

It gives this error now: callback(new MongoError(document))

What does this mean ?

/home/runner/boilerplate-mongomongoose-2/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:299
callback(new MongoError(document));