【发布时间】:2016-05-15 11:49:27
【问题描述】:
ifs 中的两个 find 调用都有以函数开头的回调(例如,文档)。 将它重构为 DRYer 的干净方法是什么? 谢谢。
if (connection_id == null) {
id_connectionsCollection.find({}, {}, function (e, docs) {
if (e) {
return callback(e);
}
var connectionDetails = null;
if (docs == null || docs.length == 0) {//if no connections found, use default from config
connectionDetails = defaultConnectionDetails
}
else {
connectionDetails = docs[0];//just get the first one
}
return callback(null, connectionDetails);
});
}
else {
id_connectionsCollection.find({name: connection_id}, {sort: {updated_at: -1}}, function (e, docs) {
if (e) {
return callback(e);
}
var connectionDetails = null;
if (docs == null || docs.length == 0) {
connectionDetails = defaultConnectionDetails;
}
else {
connectionDetails = docs[0];//just get the first one
}
return callback(null, connectionDetails);
});
}
【问题讨论】:
标签: javascript node.js refactoring dry