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.
{
"_id": {
"$oid": "5c40fe9cefd53d08dcf873c8"
},
"favoriteFoods": [
"onions",
"chicken"
],
"name": "Mary",
"age": 21,
"__v": 0
}```
not sure if there should be more than 2
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)…
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.
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…
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…
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