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?


Creating and Saving a record with MongoDB
#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.


#4

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));
};   


#5

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);
  })
};

#6

Looks like you unlocked the magic number, bro!


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

#8

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


#9

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.