gpt4 book ai didi

javascript - 尝试在我的 express ejs 应用程序中呈现 html 实体

转载 作者:行者123 更新时间:2023-11-30 21:06:56 27 4
gpt4 key购买 nike

这是我要渲染的页面

<% include ../partials/boilerplate %>



<div class="triviaContainer">
<h1>GAME TIME!!! </h1>
<% var i =0; data.forEach(question =>{ i+=1 %>

<div class="questionRow">
<div> <%= question.question %></div>
</div>
<div class="questionRow1">
<div clicked="" id="correctanswer<%=i %>" class="correctAnswer" > A: <%= question.correct_answer %></div>

<div clicked="" id="incorrectanswer<%=i %>" class="incorrectanswer<%=i %>" >B: <%= question.incorrect_answers[0].incorrect_answer %></div>
</div>
<div class="questionRow2">
<div clicked="" id="secondincorrectanswer<%=i %>" >C: <%= question.incorrect_answers[1].incorrect_answer %></div>
<div clicked="" id="lastanswer<%=i %>" >D: <%= question.incorrect_answers[2].incorrect_answer %></div>
</div>


<% }) %>
<form class="gameForm" method='POST' action='/trivia/score'>

<input type="hidden" name="points" value="" id="points"/>
<input type="hidden" name="gameid" value="<%= data[0].game_id %>"/>
<input type="hidden" name="questions" value="<%= data[0].number_of_questions %>"/>
<input type="submit" value="GET RESULTS">
</form>
</div>


<script type="text/javascript" src="javascript/triviaGameScript.js"></script>

<% include ../partials/end %>

我在 foreach 函数的 question.question 部分呈现问题。如果您查看此屏幕截图,最后一个问题有一个引用 html 实体。我有几个具有这些实体的问题,想找到一个 npm 包来解码它们。 The last question next to the fedex is where the entity is

这是我需要 html-entities npm 包的 Controller

const triviaModel = require('../models/trivia');
const authHelpers = require('../services/auth/auth-helpers')
const Entities = require('html-entities').XmlEntities;
const entities = new Entities();
const triviaapiController = {};

triviaapiController.index = (req, res) => {
console.log(req.body.game_id);
triviaModel.GetGame(req.body.game_id)
.then(data =>{
console.log(data);

res.render('trivia/trivia-index', {
data: data,
});
})
.catch(err => {
console.log(err);
res.status(500).json({ err });
});
}

trivia-index promise 中的数据部分是存储问题和答案的地方。

这是这个特定包的文档。 https://www.npmjs.com/package/html-entities 我不确定如何使用它。我应该将它应用于 ejs 文件,还是应用于传入的数据对象。任何帮助将不胜感激!

最佳答案

你的代码使用这样的包:

triviaapiController.index = (req, res) => {
console.log(req.body.game_id)
triviaModel.GetGame(req.body.game_id)
.then(data => {
console.log(data)
var decodedData = []
for (var q of data) {
var decodedQ = {
question: entities.decode(q.question),
correct_answer: q.correct_answer,
incorrect_answers: q.incorrect_answers
}
decodedData.push(decodedQ)
}

res.render('trivia/trivia-index', {
data: decodedData
})
})
.catch(err => {
console.log(err)
res.status(500).json({ err})
})
}

关于javascript - 尝试在我的 express ejs 应用程序中呈现 html 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46514897/

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