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

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

#1

I am having a syntax error here. What can I be doing wrong?

I’ve also added

const Model = mongoose.Model;

on top of the myApp.js

var createManyPeople = function(arrayOfPeople, done) {
    Model.create(arrayOfPeople, (err, data) => {
      if(err) {
         done(err); 
      }
    done(null, data);
    }) 
};

https://learn.freecodecamp.org/apis-and-microservices/mongodb-and-mongoose/create-many-records-with-model-create


#2

I could be that should be mongoose.model instead of mongoose.Model.

Also, did you create a Schema?
I wrote first a Schema and then passed it to the model like mongoose.model(‘Person’, personSchema).
And then you use Person.create(…).

Here is a link to mongoose documentation for models.
http://mongoosejs.com/docs/models.html


#3

Thanks I got it. Schema was already created as part of the previous solution. I thought I had to use literally Model.create instead of using an instance of model and associated create method.

Thanks!


#4

var Model = mongoose.model;
var createManyPeople = function(arrayOfPeople, done) {
Model.create(arrayOfPeople, (err, data) => {
if(err) {
done(err);
}
done(null, data);
})
};

what is the wrong with my code.


#5

Don’t use Mode.create.

Instead use the schema you already created in the previous exercise.

Which I remember is Person?..