How models and schema works?

Tell us what’s happening:
Hi there , I’m trying to pass this test lately but I couldn’t understand the concept of models and schemas I visited the hint link and I have few question about the solution

const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI);

/** 2) Create a 'Person' Model */
var personSchema = new mongoose.Schema({
  name: String,
  age: Number,
  favoriteFoods: [String]
});

/** 3) Create and Save a Person */
var Person = mongoose.model('Person', personSchema);

var createAndSavePerson = function(done) {
  var janeFonda = new Person({name: "Jane Fonda", age: 84, favoriteFoods: ["eggs", "fish", "fresh fruit"]});

  janeFonda.save(function(err, data) {
    if (err) return console.error(err);
    done(null, data)
  });
};

  1. What does this line do exactly
    var Person = mongoose.model('Person', personSchema);
  2. Whats the diffrence beween Schema and Model ?
  3. Why we wrote the word Person in capital letter ?
  4. What the word “done” in function(done) do ?
  5. what’s the exact job of the last line done(null, data) ?
  6. Finely Im getting this message in the consol after trying to run my code :

Your App Link

Your browser information:

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

Challenge: Create and Save a Record of a Model

Link to the challenge:

First off I’d suggest checking out the documentation which explains about both Schemas and Models.

THANKS but still don’t have answers for my question
why there is a model and schema if they are just a duplication of each other thats drives me to the second question which is the job of this line
var Person = mongoose.model('Person', personSchema);
and ofcourse the rest

  1. What the word “done” in function(done) do ?
  2. what’s the exact job of the last line done(null, data) ?
  3. Finely Im getting this message in the consol after trying to run my code

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