Create and Save a Record of a Model

I do not understand the reason why it my function is not saving the info in the db.

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

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



function createAndSavePerson(done) {
       let ney = new Person({
         name:"ney",
         age:46,
         favoriteFoods: ["baguette","eau"]
       });
          ney.save(function(data,err){
                    if(err){
                    console.log(err)
                    }else{
                     done(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 });

/** **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;

What errors do you see in the Node console?

Did you add the the environment variable and applicable value to the Secrets section?

1 Like

Yes, I have done that. My code has connected to the database, but it does not populate it. I do not understand what happening is.

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