Not accepting solution on mongodb

Keep getting the same error everytime,tried lots of ways to write the code.

error message

Creating an instance from a mongoose schema should succeed (Test timed out)

this is my code

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

mongoose.connect(process.env.MONGO_URI);

var personSchema = new Schema({
  name: { type: String, required: true },
  age: Number,
  favoriteFoods: [String]
});
const Person = mongoose.model("Person", personSchema);
var createAndSavePerson = function(done) {
  const person = new Person({
    name: "Nikhil",
    age: 20,
    favoriteFoods: ["pizza", "burger"]
  });
  person.save((err, data) => {
    if (err) {
      return done(err);
    }
    return done(null, data);
  });
};

pls help

Challenge: undefined

Link to the challenge:

Hello!

Even though your code is correct, you’re not required to actually store data in this challenge, just an instance of the schema. You should remove the createAndSavePerson.

Besides that, the Schema may not be declared, so that will give you another error (unless you declared it, of course).

I removed the createAndSavePerson still the error persist

isnt this how schema is declared

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

Yes, the schema is correctly defined.

However, if you don’t create a reference to the mongoose.Schema, an error will arise because it doesn’t exist.

Check the console logs on glitch.com, on the left panel, at the bottom, there is a button labeled Tools; click there an then on Logs.

(Possible) Solution

You should try to solve it by yourself, but if you fail to solve it, try the following:

const mongoose = require('mongoose');
// Add the following right after you require mongoose:
const Schema = mongoose.Schema;

those are already added, and the logs are error free, the site which should load is also working

Oh! You’re right, I don’t know why I didn’t see that…

Turns out the problem is from the challenge: you’re using a const instead of a var, in other words, change the const Person = to var Person =.

Sorry for the inconveniences :sweat:!

I guess the test cases needed to be improved as I was not allowed to use this line.
var Person = mongoose.model(“person”, personSchema)

but actually it wanted

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

2 Likes

Thnaks droidpulkit for providing the solution! Such a small change, made all the difference :slight_smile:

I would suggest to change this lesson a bit as well, as I also thought that one should actually create and save a new person in this step.