2016-12-21 5 views
0

私はnodejsのための独自のモジュールを書く方法を学びます。具体的にはさまざまなオブジェクトを実装して、アプリケーション全体で作業することができます。コールバック関数を持つmodule.exportsを持つオブジェクトをエクスポートする

私は結果にこのような何かを使用したい:

//assuming i have database: 
Person_table(ID int A_I, NAME varchar, AGE int) 
//code: 
var p = new Person("John", 22); 
p.writeToDatabase(); 
//the object is now written in database 

を私は次のことを試してみましたが、私の理解を超えた何らかの理由では動作しません。 私は三つのファイル宣言している:、db.js、person.jsをapp.js

db.js

var mysql = require('mysql'); 
var conn = mysql.createPool({ 
    host : 'localhost', 
    database: 'db_name', 
    user: 'user_name', 
    password : 'pass', 
    multipleStatement: true, 
    connectionLimit : 10 
}); 

conn.getConnection(function(err){ 
    if(err) throw err; 
}); 

module.exports = conn; 

person.js

var db = require('./db.js'); 

function Person(n, a) { 
    this.name = n; 
    this.age = a; 
} 

Person.prototype.writeToDatabase = function (callback) { 
    db.query("INSERT INTO Person_table(NAME, AGE) VALUES(?,?)", [this.name, this.age], function (err, rows) { 
     if (err) return callback(err); 
     else return callback(null, rows); 
    }); 
} 

module.exports = Person; 

app.js

var Person = require('./person.js') 

var p = new Person("John", 22); 

p.writeToDatabase(function(err, rows){ 
    if(err) console.log(err); 
    else console.log("written to DB"); 
}); 

私はコードに何が問題なのか助けてくれてありがとう。 ボーナスとして、私は階層化されたnodejsアプリケーションを作成するために、モジュールのエクスポートとオブジェクトプロトタイプの件に関する良い文献を尋ねたいと思います。

+1

あなたが必要とするのに ".js"を入れる必要はありません –

+0

heh、私はもっと冗長にしたいと思っていましたが、これはほとんど問題ではありません:D –

+0

いいえ、ただのコメント –

答えて

0

私はこの問題を理解しました。私の例は完璧に動作しています。

私の短いプログラムの最後に私はprocess.exit()を持っていましたので、私のアプリはタスクを終了するときに終了します。しかし、node.jsの非同期性を考慮していないので、データベースへの書き込みが完了する前にprocess.exit()が実行されたため、失敗しました。

どうもありがとうございました!

0

私があげる最初のアドバイスは次の通りである、あなたはあなたのperson.jsにこのようなあなたのコールバックを使用することができます。

Person.prototype.writeToDatabase = function (callback) { 
    db.query("INSERT INTO Person_table(NAME, AGE) VALUES(?,?)", 
     [this.name, this.age], 
     callback // Since you do nothing with `err` or `rows` 
    ); 
} 

なぜ? app.jsファイルでコールバックを処理しているためです。

次に、あなたの関数conn.getConnectionが終了するのを待たずに、またこの関数の返り値を使用しないので、動作しないと思います!ドキュメントhereを確認してください。

あなたのようなあなたのコードに再編成する必要があります

db.jsを

var mysql = require('mysql'); 

module.exports = mysql.createPool({ 
    host : 'localhost', 
    database: 'db_name', 
    user: 'user_name', 
    password : 'pass', 
    multipleStatement: true, 
    connectionLimit : 10 
}); 

person.js

module.exports = function (connection) { // You need the connection to query your database. 

    function Person(n, a) { 
     this.name = n; 
     this.age = a; 
    } 

    Person.prototype.writeToDatabase = function (callback) { 
     // The connection is used here. 
     connection.query("INSERT INTO Person_table(NAME, AGE) VALUES(?,?)", 
      [this.name, this.age], 
      callback 
     ); 
    } 

    return Person; 
}; 

app.js

var db = require('./db'); 

db.getConnection(function (err, connection) { // Once you're connected to the database, you can start using your app. 

    var Person = require('./person')(connection); // Since we export a function with a parameter, we need to require the person.js like this. 

    var p = new Person("John", 22); 

    p.writeToDatabase(function(err, rows) { 
     if(err) console.log(err); 
     else console.log("written to DB"); 
    }); 
}); 

私は明確ではないんだ場合、あなたは:)詳細を求めることができます。あなたが期待しているパターンに合わない場合は、他の方法で仕事をすることができます;)

関連する問題