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

Hi,

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

/** # MONGOOSE SETUP #
/*  ================== */

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


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

/** # MONGOOSE SETUP #
/*  ================== */

/** 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](http://mongoosejs.com/docs/guide.html).

// <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"]});

 samplePerson.save(function(err, data) {
    if (err) return console.error(err);
    done(null, data)
  });
};

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

1 Like

Sure.

https://glitch.com/~fcc-challenges-mongo-mongoose

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

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

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

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.

2 Likes

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: https://docs.atlas.mongodb.com/security-whitelist/
but i havent change the internet.

I would appriciate any help .

thanks

one small thing, but it can make you sit for hours.
MONGO_URI=‘mongodb+srv://klml:@clu0.kbzya.mongodb.net/db1?retryWrites=true&w=majority’
please remove the <> tags present in .env file.