0
私はチックタックつま先のゲームを作成しようとしていると、ユーザーのデータをデータベースに保存したいのですが、私の問題は、私は '内部サーバーのエラーメッセージ(500)'が表示されます。ここで私のルートのいくつかは、エクスプレスでは動作しません
はindex.jsです:ここでは
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Lab5' });
});
//check if server is online
router.get('/alive', function(req, res, next) {
res.send('alive');
});
router.post('/alive', function(req, res) {
//here I generate the next step for the game
});
//this route can't be reached
router.get('/db', function(res, req) {
var db = req.db;
var collection = db.get('usercollection');
collection.find({}, {}, function(e, docs) {
res.send(JSON.stringify(docs));
});
});
//and this route can be reached
router.post('/db', function(req, res) {
var db = req.db;
var collection = db.get('usercollection');
var username = req.body.username;
var gameStatus = req.body.gameStatus;
try {
if(Object.keys(req.body).length !== 0 && JSON.stringify(req.body) !== JSON.stringify({})){
console.log("Data insert...");
collection.insert({
"username" : username,
"gameStatus" : gameStatus
}, function (err, docs) {
if(err) {
res.send("Error inserting data into database!");
}
});
}
} catch(err) {
console.log("Error in insert: " + err);
}
});
module.exports = router;
はgetDB.jsです:
function getDB() {
var xhttp = createRequest();
if(xhttp === null) {
alert("Ajax object not supported by your browser!");
}
else {
xhttp.onreadystatechange = function() {
if(xhttp.readyState == 4 && xhttp.status == 200) {
if(xhttp.responseText != null) {
var db = JSON.parse(xhttp.responseText);
console.log(db);
}
}
}
xhttp.open('GET', 'db', true);
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.send();
}
}
私の問題は
router.get('/db', function() {...});
と
router.post('/db', function() {...});
であります
は正常に機能し、送信されたデータをデータベースに挿入します。
助けていただけたら幸いです!
「res」でメソッドを呼び出さないかのようです。あなたがコード全体を表示していないか、またはこれが問題になるかもしれないからですか? –
はい、今は動作します。唯一の問題は、resとreqがパラメータリストで逆になっていたことです。 – blacklight93