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

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:

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

Does the schema look like this:

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.

my schema is:

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:


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)?

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:

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

and

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

not sure if there should be more than 2

Can you post a link to your glitch editor

Your code:

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

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

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

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>

?

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)…

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

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()

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.

2 Likes

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

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…

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

Glad it’s working.
Good luck.

1 Like

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

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…

1 Like

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

2 Likes

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

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