M.Voloskov M.Voloskov - 22 days ago 8
Node.js Question

How do I refresh Mongoose models?

The task is:


  1. delete all model's documents

  2. then save the new model's documents



Something like

Model.remove().then(function () {
Model.save(new Model({ *some valid data* });
});


It removes all model documents as expected, but the new document doesn't appear, the documents list is empty. What's the problem?

Answer

Current Code:

Model.remove().then(function () {
new Model({ *some valid data* }).save();
});

model.save() creates a promise object. you need to call .then() on it for the promise to actually run

Change to:

Model.remove().then(function () {
new Model({ *some valid data* }).save().then(function () {
// log something here
});
});