简单的塞内卡承诺示例

时间:2017-03-28 18:12:44

标签: node.js bluebird seneca

我正在尝试将Bluebird与Seneca一起使用以满足我创建的所有seneca模块。我希望能够连接塞内卡命令。

这是一个事务模块,它使事情变得尽可能简单,只有一个任务,来创建一个事务

// ./app/transaction.js

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

  var Transaction = require('./models/transaction');

  this.add({role:'transaction', cmd: 'create'}, function( msg, done ) {
    var t = new Transaction({
      datetime: new Date(),
      stylist_ids: ["STY_001", "STY_002"],
      promoter_ids: ["HOT_001", "HOT_002"],
      amount: 100
    });

    t.save((err,transaction) => {
      if (err)
        done(err);
      else
        done(null, { transaction: transaction });
    });
  })
}

这是我的server.js代码,目前应该使用seneca命令创建一个事务:

// server.js

var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/promisfy");

var seneca = require('seneca')();
var Promise = require('bluebird');
var act = Promise.promisify(seneca.act, seneca);

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

act({role:'transaction', cmd: 'create'})
  .then(function (result) {
    console.log(result)
  })
  .catch(function (err) {
    console.log(err)
  });

但是我收到了这个错误:

TypeError: Cannot read property 'ge' of undefined
    at do_act (/Users/jackrobson/development/active/glam-army-admin-app/node_modules/seneca/seneca.js:1046:30)
    at Object.api_act (/Users/jackrobson/development/active/glam-army-admin-app/node_modules/seneca/seneca.js:867:5)
    at Object.tryCatcher (/Users/jackrobson/development/active/glam-army-admin-app/node_modules/bluebird/js/release/util.js:16:23)
    at ret (eval at makeNodePromisifiedEval (/Users/jackrobson/development/active/glam-army-admin-app/node_modules/bluebird/js/release/promisify.js:184:12), <anonymous>:14:23)
    at Object.<anonymous> (/Users/jackrobson/development/active/glam-army-admin-app/server.js:11:1)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:418:7)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:533:3

任何帮助表示赞赏。谢谢:))

1 个答案:

答案 0 :(得分:0)

“请注意,Bluebird v3有一些promisification API更改。而不是

var act = Promise.promisify(seneca.act, seneca);

我们必须使用

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

server.js有旧命令-_-''