2016-08-08 9 views
0

私はHerokuでデータベースを設定しましたが、2つのレコードを持つusersという名前のテーブルを作成しましたが、Expressを介してNodeアプリケーションにデータを取得しようとしています。エクスプレスでMySqlからデータを取得

私は私のapp.jsファイル内のような接続設定した

:このインデックスで

var express = require('express'); 
var router = express.Router(); 

/* GET home page. */ 
router.get('/', function(req, res, next) { 
    // res.render('layout', { title: 'Movieseat' }); 

    connection.connect(); 

    connection.query('SELECT * FROM `users` WHERE `first_name` = "Kees"', function (error, results, fields) { 
     // error will be an Error if one occurred during the query 
     // results will contain the results of the query 
     // fields will contain information about the returned results fields (if any) 
     console.log(results); 
    }); 

    connection.end(); 
}); 

module.exports = router; 

// connect to the heroku database 
var mysql = require('mysql'); 
var connection = mysql.createConnection({ 
    host  : 'us-cdbr-iron-***-**.cleardb.net', 
    user  : 'bfe4***0ede74', 
    password : '6742****', 
    database : 'heroku_****ee0f0e9102' 
}); 

私はその後、index.jsファイルをルートフォルダを持っているがルートfirst_name Keesのレコードを提供しようとしています。私は私のホストを訪れたとき、私は次のエラーを取得する:

connection is not defined

ReferenceError: connection is not defined

だからそれはconnectionは私のルート・ファイル内の参照を持たないように見えますが、私は、私は私のアプリを取得、接続をクリックしてください+ CTRL私のWebStormのIDEに。私の接続を定義するjsファイル。では、ルートファイルの接続をどのように参照するのですか?私は私のindex.jsルートファイルに次の行のコメントを解除したときに

はまた:

res.render('layout', { title: 'Movieseat' }); 

私はエラーを取得する:

Error: Can't set headers after they are sent. What would be the propper way to request data and render a jade template?

答えて

1

どこかにあなたが解像度を呼び出しているため、2番目のエラーの可能性があります.send()またはres.end()またはres.render()は既にそれを認識していません。ミドルウェアなどをチェックして、そうしていないことを確認します。

最初の問題は、接続ファイルから接続オブジェクトをエクスポートすることも、ルーターファイルで接続オブジェクトを要求することもないためです。モジュールを参照するには、モジュールを明示的に要求する必要があります。

// connect to the heroku database 
var mysql = require('mysql'); 
var connection = mysql.createConnection({ 
    host  : 'us-cdbr-iron-***-**.cleardb.net', 
    user  : 'bfe4***0ede74', 
    password : '6742****', 
    database : 'heroku_****ee0f0e9102' 
}); 
module.exports = connection; 

この場合、常に同じ接続があり、スケーラビリティにはそれほど優れていません。接続プーリングを見て、グローバルオブジェクトを渡すのではなく接続を取得するメソッドをエクスポートすることを検討してください。お使いのルータのその後

var express = require('express'); 
var router = express.Router(); 
var connection = require('./path/to/your/connection.js'); 

/* GET home page. */ 
router.get('/', function(req, res, next) { 
    // res.render('layout', { title: 'Movieseat' }); 

    connection.connect(); 

    connection.query('SELECT * FROM `users` WHERE `first_name` = "Kees"', function (error, results, fields) { 
     // error will be an Error if one occurred during the query 
     // results will contain the results of the query 
     // fields will contain information about the returned results fields (if any) 
     console.log(results); 
    }); 

    connection.end(); 
}); 

module.exports = router; 
+1

最初の要求の後に動作しません - mysqlの接続が切断した後、再利用可能ではありません、あなたは新しいものを作成する必要があります(またはルートハンドラに近い/接続しないでくださいまたは接続の代わりに接続プールを使用する - 最後のオプションが優先されます) –

+0

私は同意しますが、私はテキストで多く述べましたが、彼が尋ねたことを解決することによって概念的に彼を開始しようとしていました。 – Paul

関連する問題