Wednesday, 15 March 2017

Senecajs | app esponded with result that was not an object or array: Promise

Trying to integrate promises into my seneca modules.

Firstly, we have the server.js file which exposes a route:

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");

And then we've got the payroll module (payroll.js) which contains the start of some accounting features:

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)
      });
  });

}

Any help appreciated.



via Jack Robson

No comments:

Post a Comment