Person model is not correct: mongoose

Person model is not correct: mongoose
0

#1

This is my code


and this is the challange

https://learn.freecodecamp.org/apis-and-microservices/mongodb-and-mongoose/create-a-model/


#2

Don’t use new mongoose.Schema

Use new Schema


#3

Hi,

The challenge is asking you to specify a default value, like this:

const schema = new Schema( {
  name: {
    type: String,
    required: true
  },
  age: Number,
  favoriteFoods: {
    type: [ String ],
    default: 'Pizza, of course'
  }
} );

#4

mongoose.Schema should be required unless you save mongoose.Schema into a new variable named Schema, like this: const Schema = mongoose.Schema. Otherwise Node won’t know what Schema is, right?


#5

Actually you are right,

I assumed he defined mongoose.Schema in to Schema already. Good catch, therefore @seinfeld70 you can ignore what I said above.


#6

I always make a copy to a new variable too. It looks cleaner to me :slight_smile:


#7
const Schema = mongoose.Schema;

var Person = new Schema({
  name: {type: String, required: true},
  age: {type: Number, default: 23},
  favoriteFoods: {type: [String], default: 'Pizza'}
});

This code is still giving me incorrect Person model.
Please help.


#8

I have the same problem.

This is my code:
const Schema = mongoose.Schema;
const Person = new Schema({
name: {
type: String,
required: true,
default: ‘Cesar’
},
age: {
type: Number,
default: 23
},
favoriteFoods: {
type: [String],
default: ‘Pizza’
}
});

And the error is: Person Model is not correct

I dont know what can I do.

Help me please. Thank you.


#9

Now I found the solution. You need to do That.

const Schema = mongoose.Schema;
const personSchema = new Schema({
name: {
type: String,
required: true
},
age: {
type: Number
},
favoriteFoods: {
type: [String],
default: ‘Pizza’
}
});
var Person = mongoose.model(‘Person’, personSchema);

This works.