Javascript 为什么脚本标记中的任何内容在ejs文件中不起作用?
我在Mongoose中有一个post模式,其中有一个名为Javascript 为什么脚本标记中的任何内容在ejs文件中不起作用?,javascript,node.js,mongoose,ejs,Javascript,Node.js,Mongoose,Ejs,我在Mongoose中有一个post模式,其中有一个名为isReviewed的变量。在unreviewPosts.ejs文件中,我有一个按钮,在单击时使isReviewed=true,因此我将脚本与单击事件侦听器一起使用,但在单击时什么也没有发生 我知道我可以使用AJAX请求路由,在路由中我可以轻松地更改变量的值,但是仅仅为了更改变量的值而发出请求不是会增加开销吗 所以我只想更新这个脚本中的变量,所以我怎么做呢 获取路线:- router.get('/unreviewedposts', midd
isReviewed
的变量。在unreviewPosts.ejs文件中,我有一个按钮,在单击时使isReviewed=true
,因此我将脚本与单击事件侦听器一起使用,但在单击时什么也没有发生
我知道我可以使用AJAX请求路由,在路由中我可以轻松地更改变量的值,但是仅仅为了更改变量的值而发出请求不是会增加开销吗
所以我只想更新这个脚本中的变量,所以我怎么做呢
获取路线:-
router.get('/unreviewedposts', middleware.isAdmin, middleware.isAuditor, (req,res)=>{
Post.find({}, (err,allposts)=>{
if(err) console.log(err)
else{
res.render("unreviewedposts", {posts: allposts})
}
})
})
unreviewPosts.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>unreviewed posts of all the subjects are here </h1>
<% if(currentUser !== null && currentUser.role === "admin" || currentUser.role === "auditor"){ %>
<p><%=currentUser.username%> with <%=currentUser.role%> role is logged in!</p>
<br><br><br>
<% posts.forEach((post)=>{ %>
<h1> <%=post.title%> </h1>
<h3> <%=post.publishDay%> <%=post.publish_date%> </h3>
<h3>published by <%=post.author.username%></h3>
<h4>this post is of <%=post.subject%></h4>
<p><%-post.sanitizedHtml%> </p>
<div>
<span>Likes: <%=post.likes%></span>
<span>Views: <%=post.views%></span>
</div>
<button id="approve">approve this post and send to respective posts page</button>
<script>
var approveButton = document.getElementById('approve');
approveButton.addEventListener('click', ()=>{
<%=post.isReviewed%> = true;
console.log(<%=post.isReviewed%>);
})
</script>
<!-- here we put the operations on these posts by th admins or auditors -->
<a href="/posts/<%=post._id%>/edit">Edit this post</a>
<form action="/<%=post._id%>?_method=DELETE" method="POST">
<button type="submit">Delete post</button>
</form>
<br><br><br><br><br>
<% }) %>
<% } %>
</body>
</html>
文件
所有主题的未查看帖子都在这里
角色已登录
{ %>
出版人
这篇文章很有趣
喜欢:
意见:
批准此帖子并发送到相应的帖子页面
var approveButton=document.getElementById('approve');
approveButton.addEventListener('单击',()=>{
=正确;
console.log();
})
删除帖子
如果您想对数据进行永久性更改,您必须将其发布,否则对的更改只会在本地用户的浏览器中存在,并且不会被其他任何人看到,并且在他们重新加载/离开页面后会丢失。不要太担心开销。对于用户来说,使用这种方法是非常常见的更新数据
至于您的实际代码,假设post.isReviewed
是一个布尔值,您不能执行以下操作:
<%=post.isReviewed%> = true;
或:
这显然是无效的
相反,您必须执行以下操作:
let postIsReviewed = <%=post.isReviewed%>;
let postsreview=;
然后在事件处理程序中修改该变量
false = true;
let postIsReviewed = <%=post.isReviewed%>;