【发布时间】:2011-08-25 11:45:41
【问题描述】:
我正在尝试使用 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;
以及我用于测试的简单代码:
orm = require('./orm');
model = new orm.Model();
model.getDB(function (db) { console.log("callback'd"); });
看起来不错(至少对我而言),但是 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)
经过进一步调查,如果在创建数据库对象之前/期间失败。我不知道为什么会这样。我首先认为这是因为我使用的是 git node.js 版本,但当前版本(v0.4.7)也失败了。
我正在尝试在 node.js 代码中追踪这个问题,目前没有成功。
【问题讨论】:
标签: javascript mysql node.js