# Create a Record in MongoDB

**URL:** <https://forum.freecodecamp.org/t/create-a-record-in-mongodb/376898>\
**Category:** General\
**Created:** [April 21, 2020, 8:20pm UTC](https://forum.freecodecamp.org/t/create-a-record-in-mongodb/376898 "2020-04-21T20:20:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yudikahn8](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yudikahn8/32/136895_2.png) [@yudikahn8](https://forum.freecodecamp.org/u/yudikahn8)\
**Post date:** [April 21, 2020, 8:20pm UTC](https://forum.freecodecamp.org/t/create-a-record-in-mongodb/376898/1 "2020-04-21T20:20:12Z")

</div>

hello there !!  
this is my first time of asking here a question, so i hope it came out good. 🙃  
i am stuck in the " Create and Save a Record of a Model" challenge in the mongoDB challenges.  
my code looks like that:

```auto
const mongoDB = require("mongodb");
const mongoose = require("mongoose");
process.env.MONGO_URI="mongodb+srv://yudikahn:<myPasswordHere>@fcc-myfirstcluster-fecus.mongodb.net/test?retryWrites=true&w=majority";
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

var PersonSchem = new mongoose.Schema({
  name : {type:String, required: true},
  age : Number,
  favoriteFoods: [String]
});
var Person = mongoose.model('Person', PersonSchem);

var createAndSave = (done) => {
  var person1 = new Person({name:'dan', age:22, favoriteFoods:['a','b','c']});
   person1.save((err, data)=>err?done(err):done(null, data));
};

```

and i dont know what’s wrong with it…  
I also checked the database, and the data/object was saved successfully…  
thanks !!!

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [April 22, 2020, 10:36am UTC](https://forum.freecodecamp.org/t/create-a-record-in-mongodb/376898/2 "2020-04-22T10:36:16Z")

</div>

Welcome, yudikahn.

You are not supposed to set the `MONGO_URI` inside your script.

You are supposed to have a seperate file named `.env` that contains:

```auto
MONGO_URI='...'

```

Then, you reference that value by calling `process.env.MONGO_URI`.

Although, re-reading your post, if you managed to save the model, then maybe you did do the above?

A note about using arrow functions: **Avoid it**

There is a lot more information about this on the Mongoosejs documentation page, but I just avoid using them whenever I work with Mongoose.

Hope this helps

---

<div class="post-metadata">

**Author:** ![yudikahn8](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yudikahn8/32/136895_2.png) [@yudikahn8](https://forum.freecodecamp.org/u/yudikahn8)\
**Post date:** [April 22, 2020, 11:46am UTC](https://forum.freecodecamp.org/t/create-a-record-in-mongodb/376898/3 "2020-04-22T11:46:23Z")

</div>

thanks for taking the time to answer !  
after a while i noticed FCC already written the functions structure,  
I just had to fill them. now its working 😃  
p.s  
thanks for the arrow function note!!!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:58pm UTC](https://forum.freecodecamp.org/t/create-a-record-in-mongodb/376898/4 "2021-06-01T18:58:00Z")

</div>

Closed on June 1, 2021, 6:58pm UTC


