[英]Accessing MySQL database in node.js - internal assertion fails
我正在尝试使用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世界中似乎不希望具有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();
因此,我基本上忘记了调用new
和connect()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.