MongoDB and Mongoose - Create and Save a Record of a Model - Missing done() argument

This test has been discussed a lot over here but I feel that my issue is something new. I’ve tested all the codes mentioned by other coders here but it all ends up with “Missing done() argument” error.

One person mentioned a solution to the same error that there are two instances of the Person model but I’m not sure if this is true in my case.

Can anyone please let me know what is it that I’m missing?
Here’s my code:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

mongoose.connect(process.env.MONGO_URI);

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

const Person = mongoose.model("Person", personSchema);

var createAndSavePerson = function(done){  
var person = new Person({
    name: "Saad",
    age: 30,
    favoriteFoods: ["fast foods", "chinese"] 
  });
  person.save(function(err, data){
    if(err) {
      done(err);
    }
    else {
      done(null, data);
    }

I’d really appreciate a valuable input.

Thank you.

1 Like

So, createAndSavePerson is a function, where the argument to this function is the ‘done-function’ which is the function that you pass to person.save(), so that person.save() can call this function when it is done. This means you can simplify your code to be:

var createAndSavePerson = function(done) {
  var person = new Person({
    name: "Joe",
    age: 1,
    favoriteFoods: ["fish", "clams"]
  });
  person.save(done);
};

When calling the createAndSavePerson() function, you give it the done() function argument which you can do like this:

  createAndSavePerson(function(err, data) {
    if (err) return console.error(err);
    console.log("createAndSavePerson:", data.name, data._id);
  });

Or you can do like this which I found easier to read initially:

  let doneFunction = function (e, d) {
    if (e) return console.log(e);
    console.log("createAndSavePerson done, name:", d.name, "id:", d._id);
  };
  createAndSavePerson(doneFunction);

Ok, still the same error. However, I checked and saw that the data is being saved in MongoDB. It’s just the issue with the done() function. It’s been two days and I’m out of ideas how to pass this test.

The thing that bothers me the most is that the code is same what other coders have been using and it’s still passing but not for me. This is frustrating the hell out of me.

My current code is like this:

var createAndSavePerson = function(done){  
  const person = new Person({
    name: "Saad",
    age: 30,
    favoriteFoods: ["fast foods", "chinese"] 
  });
  person.save(function(err, data) {
    if (err) done(err);
    done(null, data)

    console.log(data + " saved to the DB");
  });  
};

Ok, as I was expecting, the error was generating because of a silly error.

In the App.js file, on line 112, there’s already another instance of createAndSavePerson. I just commented it out and voila, the code worked. Here’s the piece of code which was restricting the code to pass:

var createAndSavePerson = function(done) {
  
  done(null /*, data*/);

};

I hope anybody else facing the same error will can learn from this mistake of mine.

But is it your mistake? I fell into this exact same pit and I followed the same instruction you did which failed to mention boilerplate needed this change let alone requiring the variable to be called createAndSavePerson