2016-08-18 20 views
1

ノードjs(具体的にはexpress js)でアプリケーションを実行していて、mysqlクライアントでいくつかのデータを保存します。ノードjs + Mysql:致命的なエラーのためエンキューできません

それは完全に無問題にいくつかの時間のために働くが、その後、すべてのsudddenのは、私はこれらのerrosを得る:

Trace: Cannot enqueue Quit after fatal error. at Protocol._validateEnqueue (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\protocol\Protocol.js:218:11) at Protocol._enqueue (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\protocol\Protocol.js:135:13) at Protocol.quit (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\protocol\Protocol.js:88:23) at PoolConnection.end (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\Connection.js:255:18) at Pool._purgeConnection (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\Pool.js:259:16) at PoolConnection._removeFromPool (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\PoolConnection.js:70:8) at PoolConnection. (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\PoolConnection.js:29:12) at emitOne (events.js:77:13) at PoolConnection.emit (events.js:169:7) at PoolConnection.Connection._handleProtocolError (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\Connection.js:439:8) Trace: Cannot enqueue Query after fatal error. at Protocol._validateEnqueue (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\protocol\Protocol.js:218:11) at Protocol._enqueue (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\protocol\Protocol.js:135:13) at PoolConnection.query (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\Connection.js:214:25) at Object.fetchDatas (C:\nodejs\twelve-coiffure\public\javascripts\twelvebase.js:94:12) at C:\nodejs\twelve-coiffure\routes\index.js:7:7 at Layer.handle [as handle_request] (C:\nodejs\twelve-coiffure\node_modules\express\lib\router\layer.js:95:5) at next (C:\nodejs\twelve-coiffure\node_modules\express\lib\router\route.js:131:13) at Route.dispatch (C:\nodejs\twelve-coiffure\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\nodejs\twelve-coiffure\node_modules\express\lib\router\layer.js:95:5) at C:\nodejs\twelve-coiffure\node_modules\express\lib\router\index.js:277:22 C:\nodejs\twelve-coiffure\public\javascripts\twelvebase.js:26 throw(message); ^

Error: Cannot enqueue Query after fatal error. at Protocol._validateEnqueue (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\protocol\Protocol.js:199:16) at Protocol._enqueue (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\protocol\Protocol.js:135:13) at PoolConnection.query (C:\nodejs\twelve-coiffure\node_modules\mysql\lib\Connection.js:214:25) at Object.fetchDatas (C:\nodejs\twelve-coiffure\public\javascripts\twelvebase.js:94:12) at C:\nodejs\twelve-coiffure\routes\index.js:7:7 at Layer.handle [as handle_request] (C:\nodejs\twelve-coiffure\node_modules\express\lib\router\layer.js:95:5) at next (C:\nodejs\twelve-coiffure\node_modules\express\lib\router\route.js:131:13) at Route.dispatch (C:\nodejs\twelve-coiffure\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\nodejs\twelve-coiffure\node_modules\express\lib\router\layer.js:95:5) at C:\nodejs\twelve-coiffure\node_modules\express\lib\router\index.js:277:22

問題は、問題は私のコードではどこから来るのか、私は本当に見つけることができないということで、私は時には時にはうまくいかないことも時には時々分からない。ここで

は、データベースを起動するために私のコードです:もちろん

var mysql = require('mysql'); 
var dbPool = mysql.createPool({ 
connectionLimit: 50, 
waitForConnections: true, 
queueLimit: 0, 
host: '*my host*', 
user: '*me*', 
password: '*my password*', 
database: '*heroku database*', 
wait_timeout:28800, 
connect_timeout:10, 
debug:true 
}); 

dbPool.getConnection(function(err, connection) { 
if (err) throw (err); 

var db = connection; 

fetchDatas = function(callback) { 

    // openDatabase(); 

    var servicesFemmes, servicesHommes, products; 

    db.query("SELECT * FROM servicesFemmes", function(err, servicesFemmes, fields) { 

     if (err) { 
      callback(sendError(err)); 
     } else { 

      db.query("SELECT * FROM servicesHommes", function(err, servicesHommes, fields) { 
       if (err) { 
        callback(sendError(err)); 
       } else { 
        db.query("SELECT * FROM produits", function(err, products, fields) { 
         if (err) { 
          callback(sendError(err)); 
         } else { 
          db.query("SELECT * FROM carousel", function(err, carousel, fields) { 
           if (err) { 
            callback(sendError(err)); 
           } else { 
            callback(null, servicesFemmes, servicesHommes, products, carousel); 
           } 

          }); 
         } 
        }); 
       } 
      }); 

     } 
    }); 

} 
} 
} 

私は、すべての件のデータを表示するために取得するために別のファイルからfetchDatas()を呼んでいます。

私は本当に失われており、既存のフォーラムごとに4時間以上解決策を探しています。お手伝いをしていただきありがとうございます。

+0

[Sequelize](http://sequelizejs.com)が提供しているような、ここでは "callback hell"になっているので、おそらく約束ライブラリを使いたいと思うでしょう。 – tadman

+0

プールから再度再利用できるように接続を解放しますか? + dbオブジェクトはどこから来ますか?接続オブジェクトではないはずですか? –

+0

ok @tadman私はここで解決策が見つからない場合、おそらくすぐにその真実を使用するつもりです。私は単純な(私はsqlite3から始めましたが、herokuホスティングと互換性がありませんでした)ちょっとしたお店のための単純なページなので、これを維持したかったのです。 –

答えて

1

最後に、私は単純なMYSQLを削除し、代わりにSequelizeを使用しました。ここに私のコードは、(最終的に働く)である:

var bdd = require('fileWithSequelizeInstantiation') 

bdd.synchronize(function() { 
//here I call the functions related to my database. In my case: bdd.fetchDatas() 
}) 

var mysql = require('mysql'); 
var Sequelize = require('sequelize'); 

var sequelize = new Sequelize('database name', 'username', 'password', { 
host: 'your host url', 
dialect: 'mysql', 

pool: { 
    max: 10, 
    min: 0, 
    idle: 3000 
} 
}); 


//Models' creation 
var servicesHommes = sequelize.define('servicesHommes', { 
nom: { 
    type: Sequelize.STRING, 
    field: 'nom' 
}, 
cheveuxLongs: { 
    type: Sequelize.STRING, 
    field: 'cheveuxLongs' 
}, 
cheveuxCourts: { 
    type: Sequelize.STRING, 
    field: 'cheveuxCourts' 
} 
}, { 
freezeTableName: true 
}); 

var servicesFemmes = sequelize.define('servicesFemmes', { 
nom: { 
    type: Sequelize.STRING, 
    field: 'nom' 
}, 
cheveuxLongs: { 
    type: Sequelize.STRING, 
    field: 'cheveuxLongs' 
}, 
cheveuxCourts: { 
    type: Sequelize.STRING, 
    field: 'cheveuxCourts' 
} 
}, { 
freezeTableName: true 
}); 

var carousel = sequelize.define('carousel', { 
photo: { 
    type: Sequelize.STRING, 
    field: 'photo' 
} 
}); 

var produits = sequelize.define('produits', { 
    marque: { 
    type: Sequelize.STRING, 
    field: 'marque' 
    }, 
    nom: { 
    type: Sequelize.STRING, 
    field: 'nom' 
    }, 
    photo: { 
    type: Sequelize.STRING, 
    field: 'photo' 
    } 
}, { 
freezeTableName: true 
}); 

synchronizeBdd = function(callback) { 

sequelize.sync().then(function() { 
//any function querying the database should be placed here in this callback 


    fetchDatas = function(callback) { 

     servicesFemmes.findAll().then(function(femmes) { 
      servicesHommes.findAll().then(function(hommes) { 
       produits.findAll().then(function(products) { 
        carousel.findAll().then(function(carousels) { 
         callback(null, femmes, hommes, products, carousels); 
        }) 
       }) 
      }) 
     }); 
    } 

    //end of sync() 
    exports.fetchDatas = fetchDatas; 
    callback(); 
}); 
} 

exports.synchronize = synchronizeBdd; 

それから私は(「exports.synchronize = synchronizeBdd」)コードの最後の行にエクスポート「同期」私の関数を呼び出す別のファイルを持っています

+0

ここにあるように['Promise.props'](http://bluebirdjs.com/docs/api/promise.props.html)を使ってコードをクリーンアップすることができます。これは、複数の約束を並行して実行し、その結果を組み合わせて使用​​する場合に便利です。 – tadman

関連する問題