我在 node-mongodb-native 和 EventEmitter 之间遇到了一个奇怪的问题。
我已将问题简化为以下代码:
var mongodb = require( 'mongodb' ),
Server = mongodb.Server,
Db = mongodb.Db,
events = require( 'events' ).EventEmitter.prototype;
// Create a mongo client object
var client = new Db( 'tartempion',
new Server(
'127.0.0.1',
27017
)
);
// Open the connection
client.open( function( err, db ) {
if ( err ) throw err;
database = db;
console.log( 'Database driver loaded.' );
events.emit( 'hi' );
});
// If I comment this out, I don't get the error anymore
// and the "Database driver loaded." log is displayed.
events.on( 'hi', function() {
console.log( 'hey man' );
});
我收到了这个错误:
net.js:140
// Uncomment the following lines after libuv backend is stable and API
^
RangeError: Maximum call stack size exceeded
我认为这可能与回调中的事件有关,但这段代码有效:
events.on( 'hi', function() {
console.log( 'hey man' );
});
f( function() {
events.emit( 'hi' );
});
function f( callback ) {
callback();
}
所以...我没有看到问题出在哪里。
仅供参考,我在 node-mongodb-native issue queue上交叉发布了这个。