私はexpress.jsを使用して最初のnode.jsアプリを構築してきました。楽しかったです:)私はある種の誤解が起こっているに違いないので、ここに行きます。
私はそのように定義されたいくつかのルートを持っています:
app.all('/account/summary', apiPOST('AccountSummary', {FromDate: 'default', ToDate: 'default'}), function(req, res){
var data=req.apiJSON;
console.log(data);
res.render('accountsummary', {locals: data, layout: 'layouts/layout'});
});
apiPOST()は次のように定義されます。
apiPOST = function (operation, postParams) {
return function (req, res, next){
console.log('RIGHT AT START');
console.log(postParams);
console.log('END');
var currentDate = new Date();
var day = ('0'+currentDate.getDate()).slice(-2);
var month = ('0'+(currentDate.getMonth() + 1)).slice(-2);
var year = ('0'+currentDate.getFullYear()).slice(-4);
console.log('BEFORE DATES');
console.log(postParams);
if (typeof(postParams.FromDate)!='undefined' && (postParams.FromDate=='default' || postParams.FromDate=='')){
postParams.FromDate=year+'-'+month+'-'+day;
}
if (typeof(postParams.ToDate)!='undefined' && (postParams.ToDate=='default' || postParams.ToDate=='')){
postParams.ToDate=year+'-'+month+'-'+day;
}
//automatically add all posted data to postParams
if (typeof(req.body)!='undefined'){
for (var key in req.body){
if (req.body.hasOwnProperty(key)){
postParams[key]=req.body[key];
}
}
}
// here is do some talking to an XML web service and convert it to JSON;
// we use our postParams variable to POST
next();
}
}
まず、これは問題なく機能します。GETリクエストでページに到達すると、デフォルトでFromDateとToDateの両方が今日になります。このページには、新しいFromDataとToDateを指定するために投稿できるフォームがあります。投稿されたデータは自動的にpostParamsに追加され、それも正常に機能します。
私が経験している問題は、ユーザーが次にGETを使用してページにアクセスしたときに、以前にPOSTされたデータがまだ存在しているため、デフォルトで今日ではなくそのデータになっていることです。
そのデータがまだ利用できる理由がわかりません。見た目では、投稿されているのではなく、postParamsに記憶されています。postParamsは現在グローバルですか?
ありがとう!