Mongoose Challenges - Create and Save a Record of a Model

I’ve been trying to figure out the problem and applied the same code as mentioned by other members in response to the same issue. However, my issue is that I’m receiving the following error:

Missing Callback Argument.

I’m not sure why this is happening as I believe I have all the arguments in the function. Here’s my code:

var Person = mongoose.model("Person", personSchema);
/** 2) Create a 'Person' Model */
var createAndSavePerson = function(done){
  var person = new Person({
  name: "Saad", age: 29, favoriteFoods: ["Burger", "Pizza"]
});
  person.save(function(err, data){
    if(err) {
      return done(err);
    }
    else {
    return done(null, data);
    }
  });
}

The code seems to be alright to me so I’m not quite sure what argument is the error referring to.

Thanks for your help.

Try removing returns and call the done functions directly.

Just did but no use. Here’s the modified code:

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

The issue has been resolved and just in case somebody else gets stuck at the same place with the same error, here’s the solution.

The code was alright. Nothing wrong with it whatsoever.

What caused the error is that FreeCodeCamp already has created variables and initiated the callback function on line 105 of Glitch. All I had to do was to complete that function.

However, what I did was that I created my own function on line 38.

Since Javascript is a procedural language, despite there being a valid callback function on line 38, the incomplete function on the line 105 was preferred, the system assumed that there has been no function created at all.

So I just commented out the later function.

Hope this will help.

2 Likes