nodeEventStore发布两次

时间:2014-04-20 11:51:21

标签: javascript node.js event-sourcing

我是事件来源的新手,我正在使用nodeEventStore

所以我编写了以下简单的测试控制台应用程序,我发现该事件将发布两次,任何人都可以帮我弄明白为什么?感谢。

var eventstore = require('eventstore');
var storage = require('eventstore.mongoDb');
var async = require('async');

publisher = {
    publish: function(evt) {
        console.log('publishing: ');
        console.log(evt);
    }
}

var es = eventstore.createStore();

var store = storage.createStorage({
    host: 'localhost',
    port: 27017,
    db: 'es'
});

es.configure(function() {
    es.use(store);
    es.use(publisher);
}).start();

async.series([
    function(cb) {store.connect(function(){cb(null);})},
    function(cb) {
        es.getEventStream('1', 0, function(err, stream) {
            stream.addEvent({id: '1', event:'add', payload:{}});
            stream.commit();
            cb(null);
        });
    }
]);

并且上述输出是预期的两倍:

publishing:
{ id: '1', event: 'add', payload: {} }
publishing:
{ id: '1', event: 'add', payload: {} }

顺便说一句,如果我在下面的createStorage中连接,它可以正常工作:

var eventstore = require('eventstore');
var storage = require('eventstore.mongoDb');
var async = require('async');

publisher = {
    publish: function(evt) {
        console.log('publishing: ');
        console.log(evt);
    }
}

var es = eventstore.createStore();

async.series([
    function(cb) {
        storage.createStorage({
            host: 'localhost',
            port: 27017,
            dbName: 'es'
        }, function(err, store) {
            es.configure(function() {
                es.use(store);
                es.use(publisher);
            });
            es.start();
            cb(null);
        });
    },
    function(cb) {
        es.getEventStream('1', 0, function(err, stream) {
            stream.addEvent({id: '1', event:'add', payload:{}});
            stream.commit();
            cb(null);
        });
    }
]);

1 个答案:

答案 0 :(得分:0)

您不需要致电连接...

而不是打电话:

store.connect(function(){cb(null);})

我会打电话:

es.start(function(){cb(null);})

并且在配置后不调用es.start。