【问题标题】:nodeEventStore publish twicenodeEventStore 发布两次
【发布时间】:2014-04-21 17:43:13
【问题描述】:

我是事件源的新手,我正在使用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);
        });
    }
]);

【问题讨论】:

    标签: javascript node.js event-sourcing


    【解决方案1】:

    你不需要调用 connect...

    而不是调用:

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

    我会打电话给:

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

    配置后不调用 es.start。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多