MongoDB and Mongoose - Create and Save a Record of a Model - "createPerson is not a function" error

Hi, I can’t pass this test, keep getting “createPerson is not a function” error, and I have no idea why. This is the code I have in myApp.js:

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

var Schema = mongoose.Schema;

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

var Person = mongoose.model('Person', personModel);
exports.PersonModel = Person;

var createAndSavePerson = function(done) {
  var axl = new Person({name: 'Axl', age: 45, favoriteFoods: ['Chocolate mousse']});
  axl.save((err, data) => err ? done(err) : done(null, data));
};

Can anyone please tell me what’s wrong with it?

Is this all the code you have in App.js? Because, if so, you’ve removed a part of the file that said explicitly

DO NOT EDIT BELOW THIS LINE

The code including that line and what’s below is:

//----- DO NOT EDIT BELOW THIS LINE ----------------------------------

exports.PersonModel = Person;
exports.createAndSavePerson = createAndSavePerson;
exports.findPeopleByName = findPeopleByName;
exports.findOneByFood = findOneByFood;
exports.findPersonById = findPersonById;
exports.findEditThenSave = findEditThenSave;
exports.findAndUpdate = findAndUpdate;
exports.createManyPeople = createManyPeople;
exports.removeById = removeById;
exports.removeManyPeople = removeManyPeople;
exports.queryChain = queryChain;

If you have removed all but one of these statements (you do have in your code exports.PersonModel = Person;), you won’t pass the other tests because in server.js (which you should also NOT edit), the above exports are imported and used like this:
var createPerson = require('./myApp.js').createAndSavePerson;

So, if you’ve removed those exports, and in particular for this test - exports.createAndSavePerson = createAndSavePerson; , there’s no way to pass this test or any of the subsequent ones.

Thank you, that solved “createPerson is not a function” error. Still having issues with this challenge though, now I’m getting “Timeout has occured” error again.

I’m getting a weird ‘done()’ argument is missing with your code, and my code, and everyone else’s “passing” code I can find in the forum.

var Schema = mongoose.Schema;

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

var Person = mongoose.model('Person', personModel);
exports.PersonModel = Person;

var createAndSavePerson = function(done) {
  var axl = new Person({name: 'Axl', age: 45, favoriteFoods: ['Chocolate mousse']});
  axl.save((err, data) => err ? done(err) : done(null, data));
};   

Hey is this still active thread??

I have the correct code yet it’s showing this in the test window:

My Code Snippet:

const createAndSavePerson = function(done) {
  
  let me = new Person({name: 'random guy', age: 20, favoriteFoods: ['Tacos', 'Cheetos']});
  me.save((err, data) => {
    
    if (err) return done(err);
    return done(null,data);
  })
};

Looks like you unlocked the magic number, bro!

  1. Did you edit the server.js file? (Don’t do it!)
  2. What does the rest of your App.js file look like?

what’s a magic number??

  1. Yes I edited server.js file to try to make it work because all the other excercises were working good (I’ve completed rest of the course and 4/5 projects)

  2. glitch . com/edit/#!/abiding-seagull?path=myApp.js:1:0

You’re free to do what you like, but at the very top of server.js it says:

/********************************************
 * DO NOT EDIT THIS FILE
 * the verification process may break
 *******************************************/

Anyway, as a quick check, try changing your schema so that your array in ‘favoriteFoods’ isn’t untyped.
An example from the Mongoose docs:

var ToySchema = new Schema({ name: String });
var ToyBoxSchema = new Schema({
  toys: [ToySchema],
  buffers: [Buffer],
  strings: [String],
  numbers: [Number]
  // ... etc
});

I’m not sure if it’ll fix your problem, but it caused me problems before. Once I defined favoriteFoods as an array of strings, things worked.

Hi @SirRobLyon, were you able to solve this issue because I’ve spent 2 days dealing with this weird, “Done() argument missing” error. I’ve copied code from other coders who claimed that it’s working for them but I’m facing the same error.
Never touched server.js file.

Can you tell me how did you solve this issue? Of course, if you solved it.