asynchronous - Node.js : res.redirect("back") got Error('Can\'t set headers after they are sent.'); -
I have a small problem on the express.js app, can not be redirected back after several actions with callback.
My Apps work with objects called "marker" and the new subcategory for them "is evaluated by sub-categories" The goal of these actions 2 or more subcategories All markers to merge old and Finally, remove the old sub-class.
Here is the code:
Work said after checking two or more subcategories:
exports.postMergeSubCategories = function (Rick, Ridge ) {"Strict use"; Var data = {}; Data subcategories = req.body.subCategoriesChecked; Data.enName = req.body.subCategory.enName; Data.frName = req.body.subCategory.frName; Data.deName = req.body.subCategory.deName; If (data.subCategories.length & gt; 1) {sscategoryMapper.merge (data, function (Hey, sscategory) {if (err) return Consoleklog (fault); console.log ( 'merged sub-categories!' ); Request .flash ('merge message', 'merge complete!'); Res.redirect ("back");}); } Else {// error console.log ('error while sub-categories merge. This number of sub-categories is less than 2 checked'); Req.flash ('merge message', 'An error occurred while merge'); Res.redirect ("back"); }};
function sscategoryMapper.merge:
module.exports.merge = function (data, callback) {async.waterfall ([function (callback) {/ save / create a new sub-category (data, function (Hey, Sscategory) {if (err) return callback (fault); callback (zero, Sscategoryk_id);}); function (SscategoryId, callback) {} // ' | 'marker2sscategory async.each (data.subCategories.split (update), function (OldSscategoryId, err) {// for each sub-categories, updating markers2sscategory year subcategory remove async.waterfall ([function (callback) {// update (}, function o; all Target marker marker2sscategoryMapper.update ({; callback (zero, oldSscategoryId); if (error) return callback (fault)} sscategoryId, oldSscategoryId, function (err)) of LdSscategoryId maker2sscategory to install new sscategoryId for callback) {// Removal of old sscategory removal (old sscdidid, function (error)) {if (err) return callback (err); call back ();});}], function (error) {if (err) return callback ); call back(); }); }, Function (mistake) {callback (); }); Callback (empty, sscategoryId); }], Function (fault, result) {if (return) return callback (err); Callback (faucet, result); }); }; The first problem here is: I called callback 2 times ... This part of the code was taken to:
}, function (mistake) {Callback (null, sscategoryId); }); }], Function (fault, result) {if (return) return callback (err); Callback (faucet, result); }); };
Update end
function marker2sscategoryMapper.update:
module.exports.update = function ( , callback, from) {// new subcategory updated with marker2sscategory dbMarker2sscategory.update (for { '_ sscategory': the} { '_sscategory': for} {multi-: true}, function (err) {if (err ) Return callback (new error (mistake); Callback (empty, from);}); }
Remove more functions:
var remove = module.exports.remove = function (id, callback) {dbSscategory.remove ({'_ id ': Id}, function (error) {if (mistake) return callback (new error (mistake)) callback (empty, id);}); };
If I comment on "res.redirect (" back "), then this will work. Otherwise, the error is:" Header not set after sending error ('' "2 times.
I read that one of the reasons might be a duplicate callback ... but I do not see it in my code.
Your help Appreciate.
Comments
Post a Comment