ga-wdi-boston / mongoose

An introduction to mongoose
Other
5 stars 136 forks source link

scripts/people.js should use returned promise. #23

Closed gaand closed 7 years ago

gaand commented 8 years ago

Rather than passing a callback.

payne-chris-r commented 7 years ago

@gaand are you referring to this code, and if so, was this what you were looking for? Do I need that seconde (nested) .catch() or will my first catch catch it?

db.once('open', function () {
  loadPeople()
    .then((people) => {
      // Below is the way to insert that bypasses mongoose validations
      // Person.collection.insert(people);

      // This inserts and runs the documents through mongoose validations
-      Person.insertMany(people, function(err, docs) {
-        if (err) {
-          throw err;
-        }
-        console.log(docs.length + ' documents inserted');
-        done();
-      });
+      Person.insertMany(people)
+        .then((docs)=>{
+          console.log(docs.length + ' documents inserted');
+        })
+        .catch(console.error);
    })
    .catch(console.error);
});
gaand commented 7 years ago

I believe this suffices:


db.once('open', function () {
  loadPeople()
    .then(people =>
      Person.insertMany(people)
        .then(docs =>
          console.log(docs.length + ' documents inserted')
        )
    )
    .catch(console.error);
});
jrhorn424 commented 7 years ago

@gaand What about this? (from @payne-chris-r):

db.once('open', function () {
  loadPeople()
    .then(Person.insertMany)
    .then(docs =>
      console.log(docs.length + ' documents inserted')
    )
    .catch(console.error);
});
jrhorn424 commented 7 years ago

Fixed:

db.once('open', function () {
  loadPeople()
    .then(Person.insertMany)
    .then(docs =>
      console.log(docs.length + ' documents inserted')
    )
    .catch(console.error)
    .then(done);
});
jrhorn424 commented 7 years ago

@gaand I thought done went last, am I incorrect?

jrhorn424 commented 7 years ago

Done is last.