Can't Create and Save a Record of a Model on mongoose


I’m having a problem with the “Create and Save a Record of a Model” lesson. Here is my code:

/*  ================== */

/** 1) Install & Set up mongoose */

* 3. FCC Mongo & Mongoose Challenges
* ==================================

/*  ================== */

/** 1) Install & Set up mongoose */

// Add mongodb and mongoose to the project's package.json. Then require 
// mongoose. Store your Mongo Atlas database URI in the private .env file 
// as MONGO_URI. Connect to the database using the following syntax:
// mongoose.connect(<Your URI>, { useNewUrlParser: true, useUnifiedTopology: true }); 

/** # SCHEMAS and MODELS #
/*  ====================== */

/** 2) Create a 'Person' Model */
var mongoose = require('mongoose')
var Schema = mongoose.Schema;

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

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

// First of all we need a **Schema**. Each schema maps to a MongoDB collection
// and defines the shape of the documents within that collection. Schemas are
// building block for Models. They can be nested to create complex models,
// but in this case we'll keep things simple. A model allows you to create
// instances of your objects, called **documents**.

// Create a person having this prototype :

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

// 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 the [mongoose docs](

// <Your code here >

var Person /* = <Your Model> */

// **Note**: Glitch is a real server, and in real servers interactions with
// the db are placed in handler functions, to be called when some event happens
// (e.g. someone hits an endpoint on your API). We'll follow the same approach
// in these exercises. The `done()` function is a callback that tells us that
// we can proceed after completing an asynchronous operation such as inserting,
// searching, updating or deleting. It's following the Node convention and
// should be called as `done(null, data)` on success, or `done(err)` on error.
// **Warning** - When interacting with remote services, **errors may occur** !

// - Example -
// var someFunc = function(done) {
//   ... do something (risky) ...
//   if(error) return done(error);
//   done(null, result);
// };

/** # [C]RUD part I - CREATE #
/*  ========================== */

/** 3) Create and Save a Person */
var Person = mongoose.model('Person', personSchema);

var createAndSavePerson = function(done) {
  var samplePerson = new Person({name: "Sample Person", age: 54, favoriteFoods: ["chicken", "water"]});, data) {
    if (err) return console.error(err);
    done(null, data)

It seems that no matter what I alter in “var createAndSavePerson” and/or “”, I still get the “Creating and saving a db item should succeed” error. What am I doing wrong? Thanks in advance.

1 Like

@Gamerrethink If you are using Glitch, can you post a link to the project? That way, we can look at all the code and test some things out.


You have two problems from what I can see looking at your code:

  1. You never connect to the database.
  2. You have define two functions named createAndSavePerson. When you define another function with the same name, the first definition is ignored.

Regarding the first point, are you sure I typed out the URI right?

I don’t understand what you mean in the second point.

Either you changed the code recently (or I just did not see the line with the connection), but the first issue seems to be resolved.

The second issue is still present. I have circled the two defined functions with the same name.

Ok - just removed the second one, but now I get

// running tests
Creating and saving a db item should succeed (Test timed out)
// tests completed

I just tested your code with a valid Mongo URI and it passes the tests. You are not seeing anything in the Node logs?

Tried clearing the logs and I still get the timed out error.

A quick update:

I removed the “< >” brackets from the password in the URI and it passed the tests.


Solved my problem as well! Thanks!

what to write in place of <dbname> in url?

Hello everyone,

I’m having same problem, i can not pass it.

i’m getting an error like this one for some reasons.

(node:23327) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you’re trying to access the database from an IP that isn’t whitelisted. Make sure your current IP address is on your Atlas cluster’s IP whitelist:
but i havent change the internet.

I would appriciate any help .


one small thing, but it can make you sit for hours.
please remove the <> tags present in .env file.