What does done(null, data) do and what 'data' parameter holds?

Tell us what’s happening:
I don’t understand what the parameter data inside function(err, data) holds. Also have confusion about what done(null, data) do

Your code so far

var Person = mongoose.model('Person', personSchema);

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

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Create and Save a Record of a Model

Link to the challenge:

document.save() calls your function and provides the values for err and data. If it is successful it sets data to the record it inserted in the database, which a copy of the object you gave it with extra fields.

done is a callback you provide when you call createAndSavePerson. It is called in server.js but for debugging purposes you can call it yourself with something like this:

createAndSavePerson(function(err, data){
  if (err) console.error(err);
  else console.log(data);
1 Like