每次我抛出一个查询,嵌套深度都会增加一,就像下面的代码一样。如果我知道如何将查询定义为不在操作中的函数,我的代码的可读性就会提高。
exports.getAll = function (req, res) {
client.query('SELECT * FROM tag', function (err, result, fields) {
client.destroy();
if (err) {
throw err;
}
var tag = result[0].tag;
client.query('SELECT COUNT(follow_id) AS following_tag_num FROM follow WHERE user_id = ?', [req.session.user.user_id], function (err, result, fields) {
client.destroy();
if (err) {
throw err;
}
res.render('hoge', {
title: 'Welcome to Hoge',
userInfo: req.session.user,
tag: tag,
following_tag_num: result[0].following_tag_num
});
});
});
}