Collections created in mongoDB, but no data in document

Hi all,

My collection called ‘people’ was successfully created in MongoDB database, but there is no data in the document. I have passed the challenge up the Use model.find() to Search Your Database section, but I need to know why my data/records are not being created before I proceed with other challenges.

I have to even comment out 'pers.remove() in the server.js file, thinking that the line of code may be what is ‘deleting’ my data, but the data record still does not show. See my code below:

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

//compile a model from the schema  to create a collection
const Person = mongoose.model('Person', personSchema);

//create and save a record of a Person model
const createAndSavePerson = (done) => {

   const Person = new Person({
    name: 'Bessie', 
    age: 17,
    favoriteFoods: ['cereal', 'snacks', 'pasta']
  });

  Person.save((err, data) => {
    if(err){
      console.log (err)
    } else {
      done(null, data)
    }
  });
};

Again, the above code has passed the challenge, but there is no data record in the document in the database. This is my worry!

Not sure why it would pass the challenge, but have a look at your variable naming:

const Person = new Person({ ... })

You can’t name both the mongoose model and the record the same.

Also, you are only logging the err, but you should (also) pass it to the callback done.

This does it. Code updated and the data created in the document successfully. Thank you so much.

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