Hi, I create as required the code and double-check with the help code, but so far there is no success in passing the task. I have this error:
(node:2265) UnhandledPromiseRejectionWarning: MongooseTimeoutError: Server selection timed out after 30000 ms
/**********************************************
* 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 });
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true, useUnifiedTopology: true });
/** # SCHEMAS and MODELS #
/* ====================== */
/** 2) Create a 'Person' Model */
// 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 >
let Schema = mongoose.Schema;
let personSchema = new Schema({
name: String,
age: Number,
favoriteFoods :[String]
})
/* = <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 */
// Create a `document` instance using the `Person` constructor you build before.
// Pass to the constructor an object having the fields `name`, `age`,
// and `favoriteFoods`. Their types must be conformant to the ones in
// the Person `Schema`. Then call the method `document.save()` on the returned
// document instance, passing to it a callback using the Node convention.
// This is a common pattern, all the **CRUD** methods take a callback
// function like this as the last argument.
// - Example -
// ...
// person.save(function(err, data) {
// ...do your stuff here...
// });
var Person = mongoose.model('Person', personSchema)
var createAndSavePerson = function(done) {
var Volodymyr = new Person({name: 'Volodymyr', age: 25, favoriteFoods: ['apple', 'kiwi']})
console.log(Volodymyr)
Volodymyr.save((err, data) => {
err ? console.error(err) :done(null, data)
});
};
** Link to the project itself**
Link to the challenge:
https://www.freecodecamp.org/learn/apis-and-microservices/mongodb-and-mongoose/create-and-save-a-record-of-a-model