# MongoDB - Create Many Records with model.create() - TIME OUT ERROR

**URL:** <https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249>\
**Category:** General\
**Created:** [January 17, 2019, 9:14pm UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249 "2019-01-17T21:14:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![robwuk](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/robwuk/32/105587_2.png) [@robwuk](https://forum.freecodecamp.org/u/robwuk)\
**Post date:** [January 17, 2019, 9:14pm UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/1 "2019-01-17T21:14:24Z")

</div>

I am working on the MongoDB/Mongoose courses and have reached the “Create Many Records with model.create()”. It seems to work (ie I get 2 records in the database when I run the test) but I am getting an error (believe it is a timeout but not sure). Code I came up with for the exercise is:

```auto
var arrayOfPeople=[{name:"JImmy",age:44,favoriteFood:["Fruit", "Cereal", "Italian", "Chocolate"]},
                   {name:"Johnny",age:17,favoriteFood:["BBQ", "Salad", "Italian", "Choclate"]}];

var createManyPeople = function(arrayOfPeople, done) {
    Person.create(arrayOfPeople, (data, err) =>{
      if (err) {
        return done(err);
      }
      return done(null, data);
    });
};

```

NOTE definition of the Schema and Model are earlier in the code and not changed since previous exercises passed.

Any help gratefully received

---

<div class="post-metadata">

**Author:** ![snowmonkey](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/snowmonkey/32/241579_2.png) [@snowmonkey](https://forum.freecodecamp.org/u/snowmonkey)\
**Post date:** [January 17, 2019, 9:26pm UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/2 "2019-01-17T21:26:46Z")

</div>

Does the schema look like this:

```auto
let personSchema = new mongoose.Schema({ 
  "name": String,
  "age" : Number,
  "favoriteFoods": [String]
});

```

Note that, in your testing one, you use `favoriteFood` rather than `favoriteFoods`. Not sure what you may have used in the schema, but I’d check that.

---

<div class="post-metadata">

**Author:** ![robwuk](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/robwuk/32/105587_2.png) [@robwuk](https://forum.freecodecamp.org/u/robwuk)\
**Post date:** [January 17, 2019, 9:43pm UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/3 "2019-01-17T21:43:57Z")

</div>

my schema is:

```auto
var Schema = mongoose.Schema;
var personSchema = new Schema({
  name: { type: String, required: true },
  age: Number,
  favoriteFoods: [String]});

```

and you were right on my code - changed to:

```auto

var arrayOfPeople=[{name:"JImmy",age:44,favoriteFoods:["Fruit", "Cereal", "Italian", "Chocolate"]},
                   {name:"Johnny",age:17,favoriteFoods:["BBQ", "Salad", "Italian", "Choclate"]}];

var createManyPeople = function(arrayOfPeople, done) {
    Person.create(arrayOfPeople, (data, err) =>{
      if (err) {
        return done(err);
      }
      return done(null, data);
    });
};

```

but still getting an error…

could the problem be that I still have old code uncommented (eg the previous exercises)?

---

<div class="post-metadata">

**Author:** ![robwuk](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/robwuk/32/105587_2.png) [@robwuk](https://forum.freecodecamp.org/u/robwuk)\
**Post date:** [January 17, 2019, 10:19pm UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/4 "2019-01-17T22:19:33Z")

</div>

to be clear, the response I get when I run the FCC tests is:

> // running tests
> 
> SERVER ERROR
> 
> // tests completed

timeout is only if the code has an error.

and the 2 records I end up with in my database are:

```auto
{
    "_id": {
        "$oid": "5c40fe9cefd53d08dcf873c7"
    },
    "favoriteFoods": [
        "pizza",
        "salad"
    ],
    "name": "John",
    "age": 24,
    "__v": 0
}

```

and

````auto
{
    "_id": {
        "$oid": "5c40fe9cefd53d08dcf873c8"
    },
    "favoriteFoods": [
        "onions",
        "chicken"
    ],
    "name": "Mary",
    "age": 21,
    "__v": 0
}```

not sure if there should be more than 2
````

---

<div class="post-metadata">

**Author:** ![Dereje1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dereje1/32/63598_2.png) [@Dereje1](https://forum.freecodecamp.org/u/Dereje1)\
**Post date:** [January 18, 2019, 1:38am UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/5 "2019-01-18T01:38:25Z")

</div>

Can you post a link to your glitch editor

---

<div class="post-metadata">

**Author:** ![willjw3](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/willjw3/32/196790_2.png) [@willjw3](https://forum.freecodecamp.org/u/willjw3)\
**Post date:** [January 18, 2019, 2:28am UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/6 "2019-01-18T02:28:40Z")

</div>

Your code:

```auto
Person.create(arrayOfPeople, (data, err) =>{

```

Switch the places of `data` and `err`, and you should pass.

---

<div class="post-metadata">

**Author:** ![robwuk](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/robwuk/32/105587_2.png) [@robwuk](https://forum.freecodecamp.org/u/robwuk)\
**Post date:** [January 18, 2019, 7:11am UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/7 "2019-01-18T07:11:16Z")

</div>

changed the parameter order but still failing. My glitch is:

> **[Glitch](https://glitch.com/edit/#!/roasted-psychiatrist?path=myApp.js:91:27)**
>
> Combining automated deployment, instant hosting & collaborative editing, Glitch gets you straight to coding so you can build full-stack web apps, fast

---

<div class="post-metadata">

**Author:** ![willjw3](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/willjw3/32/196790_2.png) [@willjw3](https://forum.freecodecamp.org/u/willjw3)\
**Post date:** [January 18, 2019, 7:59am UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/8 "2019-01-18T07:59:35Z")

</div>

In your `.env` file, all I can see is:  
`MONGO_URI=`

Is there actually a URI there? Something on the right-hand side of the ‘equals’ symbol that looks like:

> mongodb://dbuser:dbpassword@ds0\<PORT\>.mlab.com:\<PORT\>/\<DATABASE-NAME\>

?

---

<div class="post-metadata">

**Author:** ![robwuk](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/robwuk/32/105587_2.png) [@robwuk](https://forum.freecodecamp.org/u/robwuk)\
**Post date:** [January 18, 2019, 8:18am UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/9 "2019-01-18T08:18:31Z")

</div>

yeah its there:

MONGO\_URI=“mongodb:// **USER** : **PASSWORD** @ **mongodb** /\*\*database&&”

where the userid & password are correct for me and the rest is copied from the MongoDB link… as I said, I get 2 records in the database when I run the test script (dont know how many results I should be expecting)…

---

<div class="post-metadata">

**Author:** ![willjw3](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/willjw3/32/196790_2.png) [@willjw3](https://forum.freecodecamp.org/u/willjw3)\
**Post date:** [January 18, 2019, 8:41am UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/10 "2019-01-18T08:41:01Z")

</div>

Also, I checked your code in `App.js`, and the parameters were still reversed.

```auto
var createManyPeople = function(arrayOfPeople, done) {
    Person.create(arrayOfPeople, (data, err) =>{
      if (err) {
        return done(err);
      }
      return done(null, data);
    });
};

```

According to Mongoose Docs:

> Here we see that the query was executed immediately and the results passed to our callback. All callbacks in Mongoose use the pattern: `callback(error, result)` . If an error occurs executing the query, the `error` parameter will contain an error document, and `result` will be null. If the query is successful, the `error` parameter will be null, and the `result` will be populated with the results of the query.

An example given at Mongoose Docs for `create()`

```auto
Candy.create(array, function (err, candies) {
  if (err) // ...

  var jellybean = candies[0];
  var snickers = candies[1];
  // ...
});

```

When I run the fcc tests with your code `(data, err)`, I get

> ‘SERVER ERROR’

When I switch the arguments to `(err, data)`, it passes.

---

<div class="post-metadata">

**Author:** ![Dereje1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dereje1/32/63598_2.png) [@Dereje1](https://forum.freecodecamp.org/u/Dereje1)\
**Post date:** [January 18, 2019, 8:43am UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/11 "2019-01-18T08:43:18Z")

</div>

your solution works for me too, once you reverse `(data, err)` in the callback as @willjw3 noted

---

<div class="post-metadata">

**Author:** ![willjw3](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/willjw3/32/196790_2.png) [@willjw3](https://forum.freecodecamp.org/u/willjw3)\
**Post date:** [January 18, 2019, 8:44am UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/12 "2019-01-18T08:44:09Z")

</div>

If you have the parameters as `(err, data)` and it’s still not passing. Check your log and see if it says `Your app is listening on port 3000`

or if you’re getting an error. You probably won’t be getting an error since, as you said, you’re getting docs in your database collection. Just a last check anyway…

---

<div class="post-metadata">

**Author:** ![robwuk](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/robwuk/32/105587_2.png) [@robwuk](https://forum.freecodecamp.org/u/robwuk)\
**Post date:** [January 18, 2019, 8:44am UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/13 "2019-01-18T08:44:12Z")

</div>

yep - hmmm reveresed all of the data, null or data, err to be null, data or err, data - ok now working - THANK YOU

---

<div class="post-metadata">

**Author:** ![willjw3](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/willjw3/32/196790_2.png) [@willjw3](https://forum.freecodecamp.org/u/willjw3)\
**Post date:** [January 18, 2019, 8:51am UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/14 "2019-01-18T08:51:03Z")

</div>

Glad it’s working.  
Good luck.

---

<div class="post-metadata">

**Author:** ![Dereje1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dereje1/32/63598_2.png) [@Dereje1](https://forum.freecodecamp.org/u/Dereje1)\
**Post date:** [January 18, 2019, 8:58am UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/15 "2019-01-18T08:58:44Z")

</div>

something definitely wrong with your app, maybe glitch related, I copied and pasted all my solutions into yours and they fail, but they all pass in mine, looking into it

---

<div class="post-metadata">

**Author:** ![robwuk](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/robwuk/32/105587_2.png) [@robwuk](https://forum.freecodecamp.org/u/robwuk)\
**Post date:** [January 18, 2019, 9:12am UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/16 "2019-01-18T09:12:17Z")

</div>

interesting. I know that I had problems with glitch before I created an account (it wouldnt pull the code form (I assume) github to start the exercises - but ever since I created an account on glitch it has worked fine and I have been able to complete the exercises up to the one above…

on a side note, now the test is passing I am getting no records in MongoDB - not sure if the test clears out documents when it completes or if that is a bug somewhere…

---

<div class="post-metadata">

**Author:** ![Dereje1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dereje1/32/63598_2.png) [@Dereje1](https://forum.freecodecamp.org/u/Dereje1)\
**Post date:** [January 18, 2019, 9:21am UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/17 "2019-01-18T09:21:31Z")

</div>

yeah, the tests perform CRUD on your db, so I think that is normal. Yes, your file works for me now as well, it must have been the `(data, err)` switch up, for future reference and anyone that may stumble upon this thread in the future, almost all mongoose callback’s first parameter is the error following node standards

> **[The Node Way - Understanding Error-First Callbacks](http://thenodeway.io/posts/understanding-error-first-callbacks/)**
>
> Node.js best practices and design patterns to answer the greater philosophical question: "What is The Node Way?"

---

<div class="post-metadata">

**Author:** ![andrebertonha](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andrebertonha/32/119377_2.png) [@andrebertonha](https://forum.freecodecamp.org/u/andrebertonha)\
**Post date:** [July 6, 2019, 6:06am UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/18 "2019-07-06T06:06:03Z")

</div>

hey here does not appear the results  
QUERY RESULTS : 0

I get no error in fcc test, but in the db atlas collection doesnt register anything,  
just posting because code is similar with this post

---

<div class="post-metadata">

**Author:** ![isacvale](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/isacvale/32/84357_2.png) [@isacvale](https://forum.freecodecamp.org/u/isacvale)\
**Post date:** [November 13, 2019, 5:06pm UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/19 "2019-11-13T17:06:50Z")

</div>

If you’ve been getting timeouts with the right code, may I suggest going to mongodbATlas \> Collections and deleting the database? Worked for me.

---

<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:** [January 16, 2021, 11:34am UTC](https://forum.freecodecamp.org/t/mongodb-create-many-records-with-model-create-time-out-error/252249/20 "2021-01-16T11:34:18Z")

</div>

Closed on January 16, 2021, 11:34am UTC


