2016-06-15 62 views
0

我有一個名爲db.js的數據庫文件,我將它導出到我的路由並運行查詢。該代碼有一個回調函數來等待ajax響應。當我運行它時永遠不會結束ajax調用一直運行。NodeJS函數不會在ajax調用上返回響應

任何人都知道爲什麼和如何解決這個問題嗎?

感謝

db.js文件

var mysql = require('mysql'); 
    var pool = mysql.createPool({ 
     connectionLimit : 100,//To update with the max of allowed connection after that the new connections are made in the queue 
     host  : 'localhost', 
     user  : 'root', 
     password : '', 
     database : 'nodejs', 
     debug : false 
    }); 
    function handle_database(query, return_data, callback) { 
    pool.getConnection(function(err,connection){ 
     if (err) { 
      connection.release(); 
      callback(null, false); 
     }  
     connection.query(query,function(err,rows){ 
      connection.release(); 
      if(!err) { 
       if(return_data){ 
        callback(null, JSON.stringify(rows)); 
       } 
      }else{ 
       callback(null, false); 
      }   
     }); 
     connection.on('error', function() {  
      callback(null, false);  
     }); 
    }); 
} 
module.exports = handle_database; 

app.js

var express = require('express'); 
var path = require('path'); 
var favicon = require('serve-favicon'); 
var logger = require('morgan'); 
var cookieParser = require('cookie-parser'); 
var bodyParser = require('body-parser'); 

var routes = require('./routes/index'); 

var app = express(); 

// view engine setup 
app.set('views', path.join(__dirname, 'views')); 
app.set('view engine', 'ejs'); 

// uncomment after placing your favicon in /public 
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); 
app.use(logger('dev')); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: false })); 
app.use(cookieParser()); 
app.use(express.static(path.join(__dirname, 'public'))); 

app.use('/', routes); 

app.post('/assign', function(req, res){ 
    var query = "INSERT INTO assigned_stops (jsons) VALUES ('"+JSON.stringify(req.body)+"')"; 
     handle_database(query, false, function(err, response){ 
      var obj = {}; 
      console.log(response); 
      if(response !== false){ 
       obj['status'] = "OK"; 
       obj['message'] = "Data successfully assigned."; 
       obj['title'] = "Success"; 
       obj['refresh'] = true; 
       res.send(JSON.stringify(obj)); 
      }else if(response === false){ 
       obj['status'] = "KO"; 
       obj['message'] = "An error occured! Try later please."; 
       obj['title'] = "Error"; 
       obj['refresh'] = false; 
       res.send(JSON.stringify(obj)); 
      } 
     }); 
}); 

// catch 404 and forward to error handler 
app.use(function(req, res, next) { 
    var err = new Error('Not Found'); 
    err.status = 404; 
    next(err); 
}); 

// error handlers 

// development error handler 
// will print stacktrace 
if (app.get('env') === 'development') { 
    app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
    res.render('error', { 
     message: err.message, 
     error: err 
    }); 
    }); 
} 

// production error handler 
// no stacktraces leaked to user 
app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
    res.render('error', { 
    message: err.message, 
    error: {} 
    }); 
}); 


module.exports = app; 

我的視圖代碼:

$.ajax({ 
    cache: false, 
    type: 'POST', 
    data: JSON.stringify(assignmentsOBJ), 
    contentType: 'application/json', 
    url: '/assign',      
    beforeSend: function() { 
     dialogRef.getModalBody().html('Request processing, please wait...'); 
    }, 
    success: function(data) { 
     dialogRef.close(); 
     var response = JSON.parse(data); 
     if(response.status === "OK") { 
      fncShowDialog(response.message, BootstrapDialog.TYPE_SUCCESS, response.title, response.refresh);  
     } 
     else if(response.status === "KO") { 
      fncShowDialog(response.message, BootstrapDialog.TYPE_ERROR, response.title, response.refresh);  
     } 
    }, 
    error: function() { 
     dialogRef.close(); 
     fncShowDialog("An error occured! Try later please.", BootstrapDialog.TYPE_ERROR, "Error", false); 
    }, 
    complete: function() { 
     dialogRef.close(); 
    } 
}); 
+0

我想你會在返回響應之前釋放連接。 – Jai

+0

'console.log(response)'在app.post? –

+0

沒有打印在控制檯上 –

回答

1

此行app.use('/', routes); overwride你的下一個路線app.post('/assign', function(req, res){})當您請求在您的路由器中「/ assign」表示應用程序搜索處理程序(./routes/index)時。 '/ assign'後刪除路由器或呼叫

+0

這是不正確的,我有另一個app.post(....它正在工作。 –