res.render将数据发送到EJS文件并在纯javascript中使用它
我在express和res.render将数据发送到EJS文件并在纯javascript中使用它,javascript,node.js,express,ejs,Javascript,Node.js,Express,Ejs,我在express和res.render()函数中都有下一个GET函数,我将要使用的数据发送到EJS文件,现在我的问题是如何在同一个EJS文件中使用纯javascript发送到EJS文件的数据? 获取功能: router.get('/:id',(req,res)=>{ 设pollId=req.params.id; Poll.findById(pollId,(err,Poll)=>{ 如果(错误)抛出错误; Vote.find(pollId,(err,votes)=>{ res.render('
res.render()
函数中都有下一个GET函数,我将要使用的数据发送到EJS文件,现在我的问题是如何在同一个EJS文件中使用纯javascript发送到EJS文件的数据?
获取功能:
router.get('/:id',(req,res)=>{
设pollId=req.params.id;
Poll.findById(pollId,(err,Poll)=>{
如果(错误)抛出错误;
Vote.find(pollId,(err,votes)=>{
res.render('vote',{user:req.user,poll:poll,voteFail:req.flash('voteFail'),vots:vots});
});
});
});代码>使用
标记不会计算表达式。改为使用
。使用
标记不会计算表达式。改用
。尝试使用
,对我有效。尝试使用
,对我有效。有三个主要的EJS标签
<% %> and <%- %> and <%= %>
<% These will run script server side - There is no output - The user will not see this %>
<%- These will inject javascript value into your html, unsanitized - you can pass html this way. %>
<%= These tags will inject the javascript value into your html, sanitized - html tags will appear as strings %>
和
如果要在浏览器控制台中登录控制台,可以使用
<script>
console.log( <%-votes%> );
</script>
log();
如果要将console.log添加到服务器端控制台,只需使用
<% console.log(votes); %>
有三个主要的EJS标记
<% %> and <%- %> and <%= %>
<% These will run script server side - There is no output - The user will not see this %>
<%- These will inject javascript value into your html, unsanitized - you can pass html this way. %>
<%= These tags will inject the javascript value into your html, sanitized - html tags will appear as strings %>
和
如果要在浏览器控制台中登录控制台,可以使用
<script>
console.log( <%-votes%> );
</script>
log();
如果要将console.log添加到服务器端控制台,只需使用
<% console.log(votes); %>
它在浏览器控制台中返回以下内容:未捕获语法错误:无效或意外token@WackThat我不确定你到底想做什么,但是在这个特定的场景中,你可以不用
标签来做
,它应该可以工作。我正在尝试使用数据制作一个Chart.js图。您只能在纯js中执行此操作。它在浏览器控制台中返回:Uncaught SyntaxError:无效或意外token@WackThat我不确定你到底想做什么,但是在这个特定的场景中,你可以不用
标签来做
,它应该可以工作。我正在尝试使用数据制作一个Chart.js图。您只能在纯js中执行此操作。