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.