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

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

#1

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?


#2

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.


#3

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.