我想找到一种不重复自己的方法。
我的大多数 /post/ 命令应该更新一些内容,然后显示与 /get/ 命令相同的内容。
目前我正在重复自己:
.get(function(req, res) {
act({
role: 'earnings',
cmd: 'get',
wc: req.params.wc,
earner_id: req.params.earner_id,
})
.then(function(earnings) {
return act({
role: 'earnings',
cmd: 'format',
earnings: earnings,
date: true,
time: true,
note: true,
total: true
})
})
.then(function(formatted_earnings) {
if (formatted_earnings.length > 0)
res.render( 'earnings-wc-earner', {
formatted_earnings: formatted_earnings
})
else
res.render('error', {
message: "No earnings found."
})
})
.catch(function (err) {
res.send(err);
})
})
.post(function(req, res) {
act({
role: 'earnings',
cmd: 'add',
wc: req.body.wc,
earner_id: req.body.earner_id,
datetime: req.body.datetime,
earnings: req.body.earnings
})
.then(function(earnings) {
return act({
role: 'earnings',
cmd: 'get',
wc: req.params.wc,
earner_id: req.params.earner_id,
})
.then(function(earnings) {
// would love to redirect to get right about now
// especially with a success message
return act({
role: 'earnings',
cmd: 'format',
earnings: earnings,
date: true,
time: true,
note: true,
total: true
})
})
.then(function(formatted_earnings) {
if (formatted_earnings.length > 0)
res.render( 'earnings-wc-earner', {
message: "Successfully added manual payment.",
formatted_earnings: formatted_earnings
})
else
res.render('error', {
message: "No earnings found."
})
})
.catch(function (err) {
res.send(err);
})
})
无论如何我可以做一些类似重定向的事情来进入第一个 .then 的 post 函数链吗?