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

Tell us what’s happening:
Describe your issue in detail here.
When completing Lesson 4, I see in my replit console that the POST is sending a collection.remove command that is clearing my collection and not saving the array, even though the test is passing. I don’t see anywhere in my code that is sending such a command. Might this be something in the backend of the challenge that is clearing my data from my database? Screenshots attached:

Your code so far
myApp.js

//Lesson 1: Install and set up Mongoose/MondoDB
require('dotenv').config();
const mongoose = require('mongoose');
const mongoConn = process.env['MONGO_URI'];
mongoose.connect(mongoConn, {useNewUrlParser: true, useUnifiedTopology: true});

//Lesson 2: Create a model
const Schema = mongoose.Schema;
const personSchema = new Schema({
  name: {type: String, required: true},
  age: Number,
  favoriteFoods: [String]
});
const Person = mongoose.model("Person", personSchema);

//Lesson 3: Create and Save Record of Model
const createAndSavePerson = (done) => {
  var ajbayerl = new Person({
    name: 'AJ Bayerl', age: 33, favoriteFoods: ['cucumbers', 'rice', 'fish']
  });
  ajbayerl.save((err, data) => {
    if (err) return console.error(err);
  });
  done(null, data)
};

//Lesson 4: create many records with model.create()
const arrayOfPeople = [
  {name: 'Whitney Bayerl', age: 32, favoriteFoods: ['donuts', 'broccoli', 'cereal']},
  {name: 'Rylan Dunn', age: 14, favoriteFoods: ['sushi', 'ramen', 'potato chips']},
  {name: 'Evie Bayerl', age: 5, favoriteFoods: ['pop tarts', 'milk', 'hot dogs']},
  {name: 'Talia Bayerl', age: 1, favoriteFoods: ['oatmeal', 'beef', 'mac & cheese']}
];
const createManyPeople = (arrayOfPeople, done) => {
  Person.create(arrayOfPeople, (err, data) => {
    if (err) return console.error(err);
    done(null, data);
  });
};

Console:

 npm start

> fcc-mongo-mongoose-challenges@0.0.1 start
> node server.js

Your app is listening on port 3000
GET
[object Object]
OPTIONS
POST
(node:80) DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.
(Use `node --trace-deprecation ...` to show where the warning was created)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0

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

Link to the challenge:

The tests behind the scenes are performing the deletion to keep the database “clean” so the tests will all work properly.

Wouldn’t that create a problem with the next module where you are using model.find() if the collection has been cleared?

No. The tests will create records behind-the-scenes and then use the code you write to validate everything works. Otherwise, you might have records that could cause a conflict with a later test.

Are they the same records created in the previous challenges or are these different records? For example, when I’m searching for the first record I added in the 3rd challenge, I’m not finding anything.

The tests for the 3rd and 4th challenge use different records for testing. I did not look at any of the other challenges’ tests but more than likely they are creating new different records, checking something with your code, and the deleting them back out of the database.

1 Like