2017-03-15 35 views
0

嘗試將承諾集成到我的塞內卡模塊。Senecajs |應用程序與結果,而不是一個對象或數組:Promise

首先,我們有一個公開的路徑文件server.js:

var express = require('express'); 
var app = express(); 
var Promise = require('bluebird'); 

var seneca = require('seneca')(); 
var act = Promise.promisify(seneca.act, {context: seneca}); 

var payroll = require ('./app/payroll'); 
seneca.use(payroll); 

var router = express.Router(); 

router.route('/') 
    .get(function(req, res) { 

    act({role:'payroll', cmd:'generate-weekly-report-data', wc: 1489363200}) 
     .then(function (data) { 
     res.json(data) 
     }) 
     .catch(function (err) { 
     res.send(err) 
     }); 

    }) 
app.use('/payroll', router); 

app.listen(3000); 
console.log("Magic happens at port 3000"); 

然後,我們已經拿到了工資模塊(payroll.js),其中包含的某些會計功能開始:

module.exports = function(options) { 
    var Promise = require('bluebird'); 
    var seneca = this; 
    var act = Promise.promisify(seneca.act, {context: seneca}); 

    var payStructure = require ('../plugins/pay-structure'); 
    seneca.use(payStructure); 

    seneca.add({role:'payroll', cmd:'generate-weekly-report-data'}, function (args, done) { 
    act({role:'pay-structure', cmd:'get'}) 
     .then(function (pay_structure) { 
     var referralPayRate = pay_structure.referralPayRate 
     var stylistPayRate = pay_structure.stylistPayRate 

     done(null, act({role:'transactions', cmd:'get_week', wc: args.wc, condense: true, totals: true })) 
     }) 
     .catch(function (err) { 
     done(err) 
     }); 
    }); 

} 

任何幫助表示讚賞。

回答

0

爲什麼當你發佈帖子時,這一切都有意義? -_-「」

答:

問題是,我回來通過我做回調的承諾是不期待一個承諾的功能,它只是想要一些JSON數據。

相反,我需要添加一個額外的鏈來獲得承諾的結果,然後將json傳遞給done。

所以不是:

act({role:'pay-structure', cmd:'get'}) 
    .then(function (pay_structure) { 
    var referralPayRate = pay_structure.referralPayRate 
    var stylistPayRate = pay_structure.stylistPayRate 

    done(null, act({role:'transactions', cmd:'get_week', wc: args.wc, condense: true, totals: true })) 
    }) 
    .catch(function (err) { 
    done(err) 
    }); 

不過是到:

act({role:'pay-structure', cmd:'get'}) 
    .then(function (result) { 
    pay_structure = result 

    return act({role:'transactions', cmd:'get_week', wc: args.wc, condense: true, totals: true }) 
    }).then(function(transactions) { 
    done(transactions) 
    }) 
    .catch(function (err) { 
    done(err) 
    }); 
相關問題