MongoDB and Mongoose - Create a Model (is it broke?)

On the above challenge, I just can’t get my code to work… database is connecting and all packages etc. are present.

I’ve copied and pasted solutions from two other similar topics and they don’t work either.

My code:

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

var Person = mongoose.model('Person', personSchema);

My code generates the error:

“model.favoriteFoods” is not an Array

When debugging, I get:

console.log(typeof Person.age) // undefined
console.log(typeof Person.favouriteFoods) // undefined
console.log(typeof Person.name) // String

Might be because FCC is looking for the American spelling of “favorite”

1 Like

Yep, that was the fix… I can’t tell you how much this one has been driving me crazy! Can’t believe I didn’t spot that one. Thanks so much! :blush:

const mongoose = require(‘mongoose’);

/** 2) Create a ‘Person’ Model */
const Schema = mongoose.Schema;
const personSchema = new Schema({
name:{type:String,required:true},
age: Number,
favoriteFoods:[String]

})
const Person = mongoose.model(‘Person’, personSchema);

please tell me somebody what is wrong with my code.

Here is my code, it work.

var Schema = mongoose.Schema;

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

var Person = mongoose.model(“Person”, personSchema);

1 Like

Aaaaah! The real issue is in the order of the variables you declare. I placed Person below personSchema and the damn thing worked.

WOW!

I did the same thing - Favourite vs Favorite.
Thanks for the solution!

Yes I had this similar issue, correcting out to the American spelling solved it.

var Schema = mongoose.Schema;

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

const Person = mongoose.model(“Person”, personSchema);