There appears to be a bug in " Create a Model" the second challenge in the MongoDB and Mongoose course in the Back End Development and APIs section.
The instructions for the challenge are:
Create a person schema called
personSchema
with the following shape:
- A required
name
field of typeString
- An
age
field of typeNumber
- A
favouriteFoods
field of type[String]
Use the Mongoose basic schema types. If you want you can also add more fields, use simple validators like required or unique, and set default values. See our Mongoose article.
Now, create a model from the
personSchema
and assign it to the existing variablePerson
.
If you note the British English spelling of the field “favouriteFoods”.
When you come to verify your solution to the challenge it posts the following form data to the path /api/mongoose-model/
name=Mike&age=28&favoriteFoods[]=pizza&favoriteFoods[]=cheese
If you note the US English spelling of the field “favoriteFoods”.
This means that if you follow the instructions and spell the field as instructed the spelling of the fields in the form data does not match the spelling of the fields in the model which means that the data doesn’t map to the model and the test fails.
For example, the following code fails when it should pass.
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
const personSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
age: { type: Number },
favouriteFoods: { type: [String] }
});
let Person = mongoose.model("Person", personSchema);
But the following code passes when it should fail:
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
const personSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
age: { type: Number },
favoriteFoods: { type: [String] }
});
let Person = mongoose.model("Person", personSchema);
To fix the spelling in the instructions could simply be changed to the US English spelling of “favorite”.