Saturday 14 January 2017

Proper way to get out of "Callback Hell" and update a record that has columns that rely on other collections/tables.

I am trying to update a user record based on a password and two dropdowns but only if they are changed. So far I can't get out of Callback Hell and this is the closest thing I have to working:router.post('/', function (req, res, next) { if (req.session.user_id) { mongoose.model('User').findById(req.session.user_id, function (err, user) { if (err) throw err; if (user) { mongoose.model('Table1').findOne({ name: req.body.t1 }, function (err, rec1) { if (err) throw err; mongoose.model('Table2').findOne({ name: req.body.t2 }, function (err, rec2) { if (err) throw err; if (req.body.password) { user.password = req.body.password; } if (user.col1 !== rec1._id) { user.col1 = rec1._id; } if (user.col2 !== rec2._id) { user.col2 = rec2._id; } user.save(function(err) { res.redirect('/settings'); // is there a scope issue here? }); }); }); } }); } }); I'm not sure how to fix this... Any and all suggestions are welcome! Thanks for the advice in advance!

Submitted January 14, 2017 at 04:17PM by DTheDeveloper

No comments:

Post a Comment