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

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

#1

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

#2

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


#3

The instructions tell you what to name the properties.

Create a person having this prototype :

  • Person Prototype -

name : string [required]

age : number

favoriteFoods : array of strings (*)


#4

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:


#5

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.


#6

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


#7

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