nodeEventStore发布两次

nodeEventStore发布两次

问题描述:

我是新来的事件源,我使用的是nodeEventStorenodeEventStore发布两次

所以我写了下面这个简单的测试控制台应用程序,我发现这个事件会发布两次,谁能帮我弄清楚为什么?谢谢。

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

您不需要拨打连接...

而不是通话:

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

我会打电话:

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

和配置后,没有要求es.start。