在node.js中访问MySQL数据库 – 内部断言失败

我正尝试使用db-mysql库从node.js连接到MySQL数据库。 一切工作都很好用示例代码,但现在我正在尝试写一个简单的ORM。

这是我的代码:

require.paths.unshift('/usr/lib/node_modules'); // default require.paths doesn't include this function Model() { this.database = null; } Model.prototype.getDB = function(callback) { if (this.database != null) callback(this.database); else { console.log("Connecting to database..."); this.database = require("db-mysql").Database({ "hostname" : "localhost", "user" : "root", "password" : "pass", "database" : "miku", }).on("ready", function() { console.log("Database ready, saving for further use."); this.database = this; callback(this); }); } } exports.Model = Model; 

简单的代码我用于testing:

 orm = require('./orm'); model = new orm.Model(); model.getDB(function (db) { console.log("callback'd"); }); 

它看起来很好(至less对我来说),但是node.js失败与内部断言:

 Connecting to database... node: /usr/include/node/node_object_wrap.h:61: void node::ObjectWrap::Wrap(v8::Handle<v8::Object>): Assertion `handle->InternalFieldCount() > 0' failed. Przerwane (core dumped) 

经过进一步调查,如果在创build数据库对象之前/期间失败。 我不知道为什么会发生这种情况。 我首先虽然这是因为我使用git node.js版本,但它与当前版本(v0.4.7)以及失败。

我试图在node.js代码中查找这个问题,目前没有成功。

哦,这是我的努力。 在JavaScript世界里似乎有Python背景是不受欢迎的;)。

数据库初始化应如下所示:

 db = require("db-mysql"); new db.Database({ "hostname" : "localhost", "user" : "root", "password" : "pass", "database" : "miku", }).on("ready", function() { console.log("Database ready, saving for further use."); this.database = this; callback(this); }).connect(); 

所以,我基本上忘了调用newconnect()