我在寫一個Node.js + Express +快速手柄應用程序。我正在從MySQL數據庫中讀取數據並將其顯示在表中。我實現分頁功能,其中「下一步」按鈕,在單擊我做一個AJAX調用我的節點JS服務器節點JS快速手柄重新加載頁面
$j('.btn-nxt').click(function(){
$j.ajax({
type: 'post',
url: '/',
data : {
action: 'next'
},
error: function(err) {
console.log('------------ Error while updating this resident: ' + err);
console.log('------------ Error while updating this resident: ' + err.message);
}
});
});
下面是我index.js路線我index.handlebars
index.js
var express = require('express');
var router = express.Router();
var callDatabase = require('../config/database.js');
var startIndex = 0;
var recordLimit = 10;
//home page route
router.get('/', function(req, res){
callDatabase.getResidents(req, res, startIndex, recordLimit);
});
router.post('/', function(req, res){
var data = req.body;
var action = data.action;
if(action == 'next') {
startIndex += recordLimit;
}
callDatabase.getResidents(req, res, startIndex, recordLimit);
res.redirect('/');
});
module.exports = router;
而這正是我在做的實際調用到我的數據庫database.js
個database.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'dbname-db.rds.amazonaws.com',
user : 'username',
password : 'password',
port: '3306',
database : 'dbname',
multipleStatements: true,
dateStrings: true
});
var residentQuery = 'SELECT idResidents, name, primaryEmail FROM Residents LIMIT ';
exports.getResidents = function(req, res, startIndex, recordLimit) {
connection.query(residentQuery + startIndex + ',' + recordLimit, function(err, rows, fields) {
if (err) {
console.log('----------------------- ERROR: Error querying records - ' + err);
console.log('----------------------- ERROR: ' + err.message);
return err;
}
for(var i in rows) {
console.log('id: ' + rows[i].idResidents + ' name: ' + rows[i].name + ' email: ' + rows[i].primaryEmail);
}
res.render('index', {
title:'Test Node App',
dbrows: rows
});
});
}
當我運行我的應用程序,第10行出現,當我點擊下一步按鈕,在AJAX調用時,我可以的conslo.log看到下一個10行database.js。但是我的觀點沒有得到更新。我試着從index.js的route.post()和database.js中調用res.render('/'),但它給了我一個錯誤 - 「發送後無法設置標題」。
如何刷新我的表格?我使用{{#each dbrows}} ... {{/ each}}填充行。