First, I am sorry for the odd name of this thread but I was not sure how to name it.
I am working on a NodeJS app and I seem to have stumble upon a bug I really don't understand.
This is an eLearning app so, at some point, an admin will create quizzes and questions for other users to answer.
The one creating a quiz also has the possibility to duplicate a quiz and edit the title and description of the quiz.
For this I created a route, /duplicatetest.js, that has a GET and a POST method.
The GET method brings the quiz data from the server and sends it to front end for display:
app.get('/duplicatetest/:idTest', isLoggedIn, hasPermision, function(req, res) {
console.log(currentDateTime.getCurrentDate() + " - GET > duplicatetest");
customLogging.customLogging("GET > duplicatetest");
var idTest = req.params.idTest;
getTestToDuplicate.getTestToDuplicate(idTest)
.then(function(result) {
res.render('duplicatetest', {
'test': result
});
});
});
The POST method saves the changes to DB:
app.post('/duplicatetest', isLoggedIn, hasPermision, function(req, res) {
console.log(currentDateTime.getCurrentDate() + " - POST > duplicatetest");
customLogging.customLogging("POST > duplicatetest");
var newTest = req.body.newTest;
saveNewTest.saveNewTest(newTest)
.then(function(result) {
console.log("Sending response now...");
res.send({
"status": 201,
"message": "",
"value": ""
});
});
});
Both seem to be working just fine. On front-end I have the following JavaScript:
$(document).ready(function()
{
$("#testName").val(test.title);
$("#testDetails").val(test.testDescription);
var url = window.location.pathname;
var idTest = url.split("/");
$("#nextStepTest").click(function()
{
const token = getCookie("u.tkn");
test.token = token;
var title = $("#testName").val();
var details = $("#testDetails").val();
var modifiedTest = {idTest: idTest[2], title: title, description: details, author: token};
var newTest = {newTest: modifiedTest}
$.ajax({
traditional: true,
url: '/duplicatetest',
type: "POST",
contentType: 'application/json',
data: JSON.stringify(newTest),
dataType: 'json',
success: function (response) {
if (response.status == 201) {
window.location = "/tests";
}
},
failure: function (response) {
console.log(response.message[1]);
}
});
});
function getCookie(name)
{
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}
});
However, I encounter the following behavior:
I visit /duplicatetest/idTest and make the changes.
Say I have a test with title Test 1 and description Test Description 1.
I change them to Test 1 Duplicate and Test description 1 Duplicate.
On saving, the data is saved in the DB without problems but instead of receiving a response and being redirected to /tests,
the page refreshes, displays the original data (Test 1 and Test Description 1) and the url changes from /duplicatetest/idTest to /duplicatetest/idTest?testName=Test+1+Duplicate (or whatever new name you pick for the new quiz).
On rare occasions however, the redirect works. I am using similar ajax calls for other pages and situations, without any problem.
What I noticed so far is that mostly there is no response from the server in front-end but so far I could not explain why.
What exactly am I doing wrong here and where is that "?testName=Test+1+Duplicate" coming from? I would really appreciate any suggestions since I've been trying to find an explanation for this for some time now...