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

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

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

#2

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.

#3

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

#4

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
#5

Can you post a link to your glitch editor

#6

Your code:

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

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

#7

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

#8

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:[email protected]<PORT>.mlab.com:<PORT>/<DATABASE-NAME>

?

#9

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

#10

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.

#11

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

#12

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…

#13

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

#14

Glad it’s working.
Good luck.

1 Like
#15

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

#16

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…

#17

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

1 Like