q******n 发帖数: 66 | 1 如果我comment out line 88, 就没问题。 各为大牛, handle_database有什么问题吗?
{code}
1 var express = require('express');
2 var app = express();
3 var mysql = require('mysql');
4 var pool = mysql.createPool({
5 connectionLimit : 100, //important
6 host : 'localhost',
7 user : 'root',
8 password : '',
9 database : 'callback',
10 debug : false
11 });
12
13 function handle_database(req,res) {
14 pool.getConnection(function(err,connection){
15 if (err) {
16 connection.release();
17 return;
18 }
19 var sql = "";
20
21 connection.query("select COUNT(*) from notifications",
function(err,rows){
22 if(!err) {
23 if(req.body.eventType == ‘TEST_START'){
24 sql = {test_id: ''' + req.body.testID + ''',
25 event_type: ''' + req.body.eventType + ''',
26 report_id: ''' + req.body.reportID + ''',
27 test_start: + req.body.testStart,
28 agent: ''' + req.body.agent + '''};
29 connection.query('INSERT INTO notifications SET
?', sql, function(err, result) {
30 if (err) throw err;
31 });
32 }
33 else if(req.body.eventType == ‘TEST_ISSUE'){
34 sql = {test_id: ''' + req.body.testID + ''',
35 event_type: ''' + req.body.eventType + ''',
36 report_id: ''' + req.body.reportID + ''',
37 timestamp: + req.body.timestamp,
38 severity: ''' + req.body.severity + ''',
39 message: ''' + req.body.message + '''};
40 connection.query('INSERT INTO notifications SET
?', sql, function(err, result) {
41 if (err) throw err;
42 });
43 }
44 else if(req.body.eventType == ‘TEST_COMPLETE'){
45 sql = {test_id: ''' + req.body.testID + ''',
46 event_type: ''' + req.body.eventType + ''',
47 report_id: ''' + req.body.reportID + ''',
48 test_end: + req.body.testEnd,
49 duration: + req.body.duration ,
50 test_status: ''' + req.body.testStatus + ''
'};
51 connection.query('INSERT INTO notifications SET
?', sql, function(err, result) {
52 if (err) throw err;
53 });
54 }
55 else if(req.body.eventType == 'REPORT_AVAILABLE'){
56 sql = {test_id: ''' + req.body.testID + ''',
57 event_type: ''' + req.body.eventType + ''',
58 report_id: ''' + req.body.reportID + ''',
59 timestamp: + req.body.timestamp ,
60 failure_message: ''' + req.body.
failureMessage + '''};
61 connection.query('INSERT INTO notifications SET
?', sql, function(err, result) {
62 if (err) throw err;
63 });
64 }
65 else {
66 console.log("ERROR: unknown messagern");
67 console.log(req.body);
68 }
69 sql = null;
70 }
71 });
72
73 connection.on('error', function(err) {
74 console.log("ERROR: unknown DB errorrn");
75 return;
76 }).setMaxListeners(0);
77 connection.release();
78 });
79 }
80
81 var port = process.env.PORT || 8080;
82 var bodyParser = require('body-parser');
83 app.use(bodyParser.json()); // support json encoded bodies
84 app.use(bodyParser.urlencoded({ extended: true })); // support
encoded bodies
85
86 // POST http://localhost:8080/callback/forfun
87 app.post('/callback/forfun’, function(req, res) {
88 handle_database(req,res);
89 res.sendStatus(200);
90 res.end();
91 }).setMaxListeners(0);
92
93 // start the server
94 app.listen(port);
95 console.log('Server started! At http://localhost:' + port);
{code} | s*i 发帖数: 5025 | 2 有可能是connection.query 还没有返回结果,提前connection.release()了。注意
javascript asynchronous的。我感觉你应该把connection.release()放到query的
callback body里。 | c*********e 发帖数: 16335 | 3 86 // POST http://localhost:8080/callback/forfun
87 app.post('/callback/forfun’, function(req, res) {
88 handle_database(req,res);
89 res.sendStatus(200);
90 res.end();
91 }).setMaxListeners(0);
说明你没弄懂node.js的异步是啥意思,还有callback.
吗?
【在 q******n 的大作中提到】 : 如果我comment out line 88, 就没问题。 各为大牛, handle_database有什么问题吗? : {code} : 1 var express = require('express'); : 2 var app = express(); : 3 var mysql = require('mysql'); : 4 var pool = mysql.createPool({ : 5 connectionLimit : 100, //important : 6 host : 'localhost', : 7 user : 'root', : 8 password : '',
| d****n 发帖数: 1637 | 4 87 app.post('/callback/forfun’, function(req, res) {
88 handle_database(req,res);
89 res.sendStatus(200);
90 res.end();
91 }).setMaxListeners(0);
==>
app.post('/callback/forfun', function(req, res){
async.series([
function handleDatabase(callback){
//db operations,
mysqlQuery = "select * from table";
query.exec(function (e ,r ){
if (e) return callback(e);
return callback(null, r);
})
},
function saveTodatabase(callback){
r.key = 1234; //mock update
r.save(callback); //if r can save
},
function closeDb(callback){
db_handler.close(callback);
}
],
function done(e, r){
if (e) return res.badRequest(e);
return res.ok(r);
});
}) | r*****i 发帖数: 234 | |
|