Sie müssten Ihre Aufrufe verschachteln, um die Produkt-ID aus dem anderen Modell zu entfernen. Etwa im Callback des Entfernens aus der Product-Collection könnten Sie auch den Verweis im Partner-Modell entfernen. Das Entfernen des Produkts löscht standardmäßig dessen Verweise auf das Campaign-Modell.
Der folgende Code zeigt die Idee:
var campSchema = require('../model/camp-schema'); router.post('/removeProduct', function (req, res) { campSchema.Product.findOneAndRemove({ _id: req.body.productId }, function (err, response) { if (err) throw err; campSchema.Partner.update( { "products": req.body.productId }, { "$pull": { "products": req.body.productId } }, function (err, res){ if (err) throw err; res.json(res); } ); }); });
Um die zugehörigen Kampagnen zu entfernen, brauchen Sie womöglich eine zusätzliche Operation, die die verknüpften Kampagnen-IDs zu einer Produkt-ID nimmt. Betrachten Sie den folgenden unsauberen Hack, der Ihnen ohne Sorgfalt beim Verschachteln der Callbacks womöglich ein Ticket in die Callback-Hölle einbringt:
router.post('/removeProduct', function (req, res) { campSchema.Product.findOneAndRemove( { _id: req.body.productId }, { new: true }, function (err, product) { if (err) throw err; campSchema.Partner.update( { "products": req.body.productId }, { "$pull": { "products": req.body.productId } }, function (err, res){ if (err) throw err; var campaignList = product.campaign campSchema.Campaign.remove({ "_id": { "$in": campaignList } }) .exec(function (err, res){ if (err) throw err; res.json(product); }) } ); } ); });
Zwar funktioniert das, doch die genannte Falle lässt sich mit async/await oder der Bibliothek async vermeiden. Um zunächst das Verständnis für mehrere Callbacks mit dem async-Modul zu fördern, ein Beispiel aus Seven Things You Should Stop Doing with Node.js für mehrere Operationen mit Callbacks, um eine Eltern-Entität und dann die zugehörigen Kinder zu finden:
methodA(function(a){ methodB(function(b){ methodC(function(c){ methodD(function(d){ // Final callback code }) }) }) })
Mit async/await wird umstrukturiert zu:
router.post('/removeProduct', async (req, res) => { try { const product = await campSchema.Product.findOneAndRemove( { _id: req.body.productId }, { new: true } ) await campSchema.Partner.update( { "products": req.body.productId }, { "$pull": { "products": req.body.productId } } ) await campSchema.Campaign.remove({ "_id": { "$in": product.campaign } }) res.json(product) } catch(err) { throw err } })
Mit dem async-Modul können Sie die Series-Methode nutzen, um verschachtelte Callbacks zu adressieren, die sonst in die Callback-Hölle führen:
async.series([ function(callback){ // code a callback(null, 'a') }, function(callback){ // code b callback(null, 'b') }, function(callback){ // code c callback(null, 'c') }, function(callback){ // code d callback(null, 'd') }], // optional callback function(err, results){ // results is ['a', 'b', 'c', 'd'] // final callback code } )
Oder die Waterfall:
async.waterfall([ function(callback){ // code a callback(null, 'a', 'b') }, function(arg1, arg2, callback){ // arg1 is equals 'a' and arg2 is 'b' // Code c callback(null, 'c') }, function(arg1, callback){ // arg1 is 'c' // code d callback(null, 'd'); }], function (err, result) { // result is 'd' } )
Zurück zu Ihrem Code: Mit der async-Waterfall-Methode könnten Sie so umstrukturieren:
router.post('/removeProduct', function (req, res) { async.waterfall([ function (callback) { // code a: Remove Product campSchema.Product.findOneAndRemove( { _id: req.body.productId }, function (err, product) { if (err) callback(err); callback(null, product); } ); }, function (doc, callback) { // code b: Remove associated campaigns var campaignList = doc.campaign; campSchema.Campaign .remove({ "_id": { "$in": campaignList } }) .exec(function (err, res) { if (err) callback(err); callback(null, doc); } ); }, function (doc, callback) { // code c: Remove related partner campSchema.Partner.update( { "products": doc._id }, { "$pull": { "products": doc._id } }, function (err, res) { if (err) callback(err); callback(null, doc); } ); } ], function (err, result) { if (err) throw err; res.json(result); // OUTPUT OK }); });