Mongoose - Create A Model

Tell us what’s happening:

Well, I am supposed to create a ‘Person’ model from a mongoose schema.
The code seems wrong for some reason, maybe I am missing something, I tried changing names and the case already.

Also please try to explain your solution as well.

Below is my code, thank you in advance!

Your code so far

const mongoose = require('mongoose');

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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36.

Challenge: Create a Model

Link to the challenge:

your codes seems right what error are you facing

check the spelling with the challenge instruction.

and

close it with ;

I am not sure, I just get the error “Creating an instance from a mongoose schema should succeed” when submitting

@Bazi-Mtsweni, did you try with spelling favoriteFoods instead of favouriteFoods?

1 Like

I fixed those, still nothing. But I’m still fighting with it

@Bazi-Mtsweni can you please share the project link?

why are you doing all this

const MongoClient = require('mongodb').MongoClient;
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const path = require('path');
const methodOverride = require('method-override');

const app = express();

var router = express.Router();

app.use(bodyParser.json());
app.use(methodOverride);
app.use(express.static(path.join(__dirname, 'Public')))```

you need only

var mongoose = require("mongoose");

Well I figured that creating an instance from the model I created was the problem, I was trying many different ways to fix it. You can amend the code if you want it’s fine as long as I’ll understand what is going on

so its done now right

Nope, i think there’s a problem beyond me this time, I even commented out every thing and left

var mongoose = require(‘mongoose’);
var Schema = mongoose.schema;
personSchema = new Schema ({
name: {
type: String,
required: true
},
age: number,
favoriteFood: [String]
});

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

then at the bottom I commented out other exports and left

exports.PersonModel = Person;

This should be working but it’s not, it still says “Creating an instance from a mongoose schema should succeed” on @freeCodeCamp.

I even saw someone on youtube who did this challenge the same way I did and it passed on his side but it fails on my side. I created my own instances on Glitch, they passed. I recreated this project locally on VS Code, used Postman to make a POST request and it worked(locally) so I don’t know really.

@Bazi-Mtsweni, compare the requirement with your code carefully

- Person Prototype -
--------------------
name : string [required]
age : number
favoriteFoods : array of strings (*)

Guys I finally found a solution, the problem was with Glitch, so I moved to repl.it, another online editor, I found out somewhere in the forum that glitch does this sometimes. Thank you for your help.