gpt4 book ai didi

mysql - 当我转到特定路线时,即使它不存在,也会呈现错误的 ejs 文件。 NodeJs

转载 作者:行者123 更新时间:2023-11-29 15:37:57 25 4
gpt4 key购买 nike

我使用调查末尾的按钮来运行 POST 操作。该路由的 POST 操作目前除了重定向到“谢谢”页面之外什么也不做。当它路由时,会呈现一个空白的调查 EJS 文件,而不是感谢 ejs。即使我将 POST 路由重定向到尚未定义的路由,也会发生这种情况。我可以执行“res.redirect('survey/ladkjfdalfkj')”而不是感谢页面,并显示空白调查页面。

我尝试将发布按钮替换为仅指向感谢页面的链接,但仍然呈现错误的文件。我什至建立了指向不存在路线的链接,尽管由于路线不存在而出现错误,但它仍然会呈现。

这是app.js

const express = require('express');
const chalk = require('chalk');
const debug = require('debug')('app');
const morgan = require('morgan');
const path = require('path');
var bodyParser = require('body-parser');


const app = express();
const port = process.env.PORT || 80;

app.use(bodyParser.urlencoded({extended:true}));

app.use(morgan('tiny'));
app.use(express.static(path.join(__dirname, 'public')));
//app.use('/css', express.static(path.join(__dirname, '/node_modules/bootstrap/dist/css')));
//app.use('/js', express.static(path.join(__dirname, '/node_modules/bootstrap/dist/js')));
//app.use('/js', express.static(path.join(__dirname, '/node_modules/jquery/dist')));
app.use('/js', express.static(path.join(__dirname, '/js')));
app.use('/css', express.static(path.join(__dirname, '/css')));
app.set('views', './src/views');
app.set('view engine', 'ejs');

const surveyRouter = require('./src/routes/surveyRoutesTest')();

app.use('/survey', surveyRouter);

app.route('/')
.get((req, res) => {
res.send('Access Forbiden')
});

app.listen(port, () => {
debug('listening on port ', chalk.green(port));
});

这是 SurveyRoutes.js

const express = require('express');
const debug = require('debug');
const surveyRouter = express.Router();
const mysql = require('mysql');
var Q = require('q');
require('dotenv').config();

const connection = mysql.createConnection({
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
host: process.env.DB_HOST
});


connection.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});


function router() {

surveyRouter.get('/:surveyGuid/', function(req, res){
//store parameters
var surveyGuid = req.params.surveyGuid;
var prms = req.query;
var date = prms.s;
var dmsId = prms.d;

Q.ninvoke(connection, "query", 'CALL spGetSurveyQuestions(?);', surveyGuid)
.then(function(rows){
// console.log(rows[0]);
questions = rows[0][0];
})
.then(function(rows){
Q.ninvoke(connection, "query", "CALL spGetSurveyQuestionValuesBySurvey(?)", surveyGuid)
.then(function(rows){
questionValues = rows[0][0]
res.render("surveyQuestionsTest", {questions: questions, questionValues:questionValues, surveyGuid: surveyGuid, prms: prms})
})
})
.catch(function(error){
console.log(error);
res.writeHead(500);
res.end();
})
.done();
});


surveyRouter.post('/:surveyGuid', function(req, res){
var surveyGuid = req.params.surveyGuid;
var prms = req.query;
var date = prms.s;
var dmsId = prms.d;
res.redirect("/survey/myself")
});

surveyRouter.get('/thankyou', function(req, res){
res.send("hello world");
});

return surveyRouter;
}

module.exports = router;

我从中删除了一些其他路线,但它们工作正常并且不关心错误。

如您所见,感谢页面应该只显示“Hello world”,但它在 res.render('surveyQuestionsTest) 处渲染页面

这是调查QuestionsTest.ejs

  <%include partials/header%>

<div id="masthead" class="container">
<div class="row py-2 shadow">
<div class="col-md-12 my-3">
<h1 class=text-center>Dental Patient Satisfaction Survey</h1>
</div>
</div>
</div>

<form action="/survey/<%=surveyGuid%>?d=<%=prms.d%>&s=<%=prms.s%>" method="POST">
<div class="containter">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="mx-5">
<div id="mainDiv" class="form-group">

<%questions.forEach(function(question){%>
<div class="row justify-content-left my-2 border-bottom">
<label class="question">
<strong><span id="questionNumber"><%=question.QustionSequence%>.</span> <%=question.QuestionText%></strong></br>
</label>
<!-- DROP DOWN HANDLING -->
<%if(question.QuestionType == "DropDown"){%>
<select class="form-control my-2 shadow-sm" name="<%=question.QustionSequence%>">
<%questionValues.forEach(function(questionValue){%>
<%if(questionValue.QuestionId == question.QuestionId){%>
<option required value="<%=questionValue.AnswerValue%>,<%= questionValue.AnswerText%>,<%= question.QuestionGuid%>,<%= question.QuestionType%>">
<%=questionValue.AnswerText%>
</option>
<%}%>
<%});%>
</select>
<%}%>

<!-- RADIO BUTTON HANDLING -->
<%if(question.QuestionType == 'RadioButton'){%>
<div class="container">
<%questionValues.forEach(function(questionValue){%>
<div class="row justify-content-left">
<%if(questionValue.QuestionId == question.QuestionId){%>
<label class="px-3" >
<input class="mx-2"
type="radio" name="<%=question.QustionSequence%>"
value="<%=questionValue.AnswerValue%>,<%= questionValue.AnswerText %>,<%= question.QuestionGuid%>,<%= question.QuestionType%>"
>
<%= questionValue.AnswerText%>
</label>
</br>
<%}%>
</div>
<%});%>
</div>
<%}%>

<!-- TEXT BOX HANDLING -->
<%if(question.QuestionType == 'Text'){%>
<div class="container">
<%questionValues.forEach(function(questionValue){%>
<%if(questionValue.QuestionId == question.QuestionId){%>
<input type="hidden" name="<%=question.QustionSequence%>" value="True,<%= questionValue.AnswerValue %>,<%= question.QuestionType%>,
<%= question.QuestionGuid%>">
<div class="row justify-content-center shadow-sm">
<textarea name="<%=question.QustionSequence%>Text" class="form-control" rows="3"></textarea>
</div>
<%}%>
<%});%>
</div>
<%}%>

</div>
<%});%>


</div>
<div class="row justify-content-center">
<button id="nextButton" class="btn btn-block btn-primary" type="submit">Submit</button>
<!-- <a href="/survey/thankyou">Submit</a> -->
</div>
</div>
</div>
</div>
</div>
</form>


<%include partials/footer%>

如果我注释掉按钮并使用提交链接,我仍然遇到同样的问题。如果该链接指向不存在的路线,它仍然会呈现空白的 SurveyQuestionsTest.ejs 文件,而不是显示错误。

我应该得到一个感谢页面,但我没有。如果我让/survey/surveyGuid 路由渲染感谢页面,那么感谢页面就会渲染得很好。

我认为它在 SurveyQuestionsTest.ejs 中,但老实说我不确定。我完全不知道该做什么。如果您需要更多信息,请告诉我。

最佳答案

非常非常愚蠢...谢谢路线被解释为/:surveyGuid 路线...

关于mysql - 当我转到特定路线时,即使它不存在,也会呈现错误的 ejs 文件。 NodeJs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58034407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com