MongoDB and Mongoose - Create Many Records with model.create()

Tell us what’s happening:

The test passes for createManyPeople but no documents appear to be created on MongoDB. The collection has no documents at all.

I do remember when I tested createAndSavePerson, it worked. UPDATE: Actually it seems that even creating one Person is not working now! :frowning: Let me look into my setup a bit more. In the meantime, I’d appreciate if you can check my code.

Sometimes I get

// running tests
Creating and saving a db item should succeed (Test timed out)
// tests completed

Setup:

  • I’m coding this locally on my machine on VS code on the Mac.
  • The service provider is Render.com.
  • I have configured the database to be accessible by all IPs.
  • I have configured the relevant MongoDB env var for my service provider.
  • Tests for previous challenges have passed.

Question:

You see where createManyPeople is defined? I am not convinced that it is referencing the correct arrayOfPeople (it’s local). Is it possible that it is reading an undefined parameter? But surely the function would fail if it’s undefined right? At the same time, my solution is pretty much the same as the fcc provided solution in the hints section. So I am confused!

I tried to do something like this, but this didn’t produce any documents either.

const phils = [ (same stuff as my code below) ];

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

Code

require("dotenv").config();

const mongoose = require("mongoose");

mongoose.connect(process.env.MONGO_URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

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

const Person = mongoose.model("Person", personSchema);

const phil = new Person({
  name: "Phil",
  age: 25,
  favoriteFoods: ["Curry", "Yogurt", "Chocolate"],
});

const createAndSavePerson = (done) => {
  phil.save(function (err, data) {
    if (err) return console.error(err);
    done(null, data);
  });
};

const arrayOfPeople = [
  {
    name: "Evil Phil",
    age: 250,
    favoriteFoods: ["Devil Fruit", "Mandrake Root", "Dark Potion"],
  },
  {
    name: "Good Phil",
    age: 90,
    favoriteFoods: ["Holy Water", "Apple of Eternal Life", "Light Potion"],
  },
  {
    name: "New Phil",
    age: 1,
    favoriteFoods: ["Milk", "More Milk", "Milk Again"],
  },
];

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

Your project link(s)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:122.0) Gecko/20100101 Firefox/122.0

Challenge Information:

MongoDB and Mongoose - Create Many Records with model.create()

Another question I have: Is there a way I can test my code locally? At the moment, I have to keep waiting for the web service to redeploy which can take up to 5 minutes everytime.

My post appears to be a duplicate of Where is my data in MongoDB? - #8 by Sky020. This means the curriculum should be clearer in saying that the records are removed after the tests. This is confusing for the student to not see anything in MongoDB.

Hello :wave:

Yes, if you clone the boilerplate locally, then you can work on the project on your own machine, and use your localhost link server to run the tests.

Here is an oldish article that outlines the workflow:

1 Like

Wow, I wish I knew this when I started the course :sweat_smile: I will give it a go!!

UPDATE: It works a charm!