简体   繁体   中英

Accessing MySQL database in node.js - internal assertion fails

I'm trying to connect to MySQL database from node.js using db-mysql library. Everything works great with example code, but now I'm trying to write a simple ORM.

This is my code:

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;

And simple code I use for testing:

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

It looks fine (at least for me), however node.js fails with internal assertion:

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)

After further investigation, if fails before/during creating Database object. I have no idea why this happens. I first though that it's because I was using git node.js version, but it fails with current release (v0.4.7) as well.

I'm trying to track down this issue in node.js code, with no success at the moment.

Oh, it was me to fail hard. It seems that having Python background is undesired in JavaScript world ;).

Database initialization should look like:

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();

So, I basically forgot to call new and connect() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM