Problem creating many records with model.create()

Tell us what’s happening:

Hey guys, I’m trying to figure this out myself but with no sucess. When I do this:

var arrayOfPeople = [
  { name: "Kafka", age: 23, favoriteFoods: ["ham"] },
  { name: "Pedro", age: 33, favoriteFoods: ["apple"] },
  { name: "Rita", age: 13, favoriteFoods: ["cake"] }
];

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

even though I am able to pass the challenge, the data doesn’t get inserted into the mongodb database. However, when I try a silly test like this:

var arrayOfPeople = [
  { name: "Kafka", age: 23, favoriteFoods: ["ham"] },
  { name: "Pedro", age: 33, favoriteFoods: ["apple"] },
  { name: "Rita", age: 13, favoriteFoods: ["cake"] }
];

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

Person.create(arrayOfPeople); //The same way I found on Mongoose documentation

the data does get inserted normally into to the db. It seems to me that there is something wrong with the createManyPeople() and done() functions.

Link to the challenge:
https://www.freecodecamp.org/learn/apis-and-microservices/mongodb-and-mongoose/create-many-records-with-model.create

1 Like

Though you should NOT permanently alter the server.js file in your project in any way, you could look up the block of code that looks like this:

var createPeople = require('./myApp.js').createManyPeople;
router.post('/create-many-people', function(req, res, next) {
  Person.remove({}, function(err) {
    if(err) { return (next(err)); }
    // in case of incorrect function use wait timeout then respond
    var t = setTimeout(() => { next({message: 'timeout'}) }, timeout);
    createPeople(req.body, function(err, data) {
      clearTimeout(t);
      if(err) { return (next(err)); }
      if(!data) {
        console.log('Missing `done()` argument');
        return next({message: 'Missing callback argument'});
      }
       Person.find({}, function(err, pers){
         if(err) { return (next(err)); }
         res.json(pers);
         Person.remove().exec();
       });
    });
  });
});

and then comment out one line of code:

var createPeople = require('./myApp.js').createManyPeople;
router.post('/create-many-people', function(req, res, next) {
  Person.remove({}, function(err) {
    if(err) { return (next(err)); }
    // in case of incorrect function use wait timeout then respond
    var t = setTimeout(() => { next({message: 'timeout'}) }, timeout);
    createPeople(req.body, function(err, data) {
      clearTimeout(t);
      if(err) { return (next(err)); }
      if(!data) {
        console.log('Missing `done()` argument');
        return next({message: 'Missing callback argument'});
      }
       Person.find({}, function(err, pers){
         if(err) { return (next(err)); }
         res.json(pers);
         // Person.remove().exec();
       });
    });
  });
});

and run the test again; you’ll pass the challenge and see documents in your collection.

The commented-out line is Person.remove().exec();

This is just fCC doing you a favor by clearing out your collection of documents. If you’re using a free, limited mongoDB account, you can see why this would be much appreciated.

Again, don’t make any permanent alterations to the server.js file.

2 Likes

Thanks for the hint mate. It’s indeed worked.

2 Likes

Thanks , was also not seeing anything in mongoAtlas