简体   繁体   English

EJS-更新数据库中的数据而无需从EJS重新加载页面

[英]EJS - Updating data in my database without reloading the page from EJS

I am creating something like the reddit comment section, someone can comment something and people can reply to that comment. 我正在创建类似reddit评论部分的内容,有人可以发表评论,然后人们可以回复该评论。

I am using Node, Express, MySQL, EJS. 我正在使用Node,Express,MySQL,EJS。

The problem: I don't know how to make the upvote/downvote part, I don't know how to make an onclick event which will tell my DB to upvote/downvote that comment by 1 without reloading the page. 问题:我不知道如何制作upvote / downvote部分,我也不知道如何创建onclick事件,该事件将告诉我的数据库以1的价格对upupte / downvote该注释,而无需重新加载页面。

Express code: 快递代码:

app.get("/posts", function(req,res){
    connection.query(`SELECT post FROM testingposts`, function(error,result){
        if(error){
            console.log(`error 757`)
        } else {
            console.log(`works btw`)              
            res.render("posts", {result: result })

        }
    })
})



app.post("/posts", function(req,res){
    let post = req.body.post;
console.log(`req.body`)
console.log(req.body.theValue)

    connection.query(`INSERT INTO testingposts(post) VALUES(?)`, [req.body.post], function(error,result){
        if(error){
            console.log(`error 555`)
        } else {

            res.redirect("/posts")

        }
    })
})

EJS: EJS:

//This part basically creates the html elements for the comments. 
//But I haven't added the upvote/downvote elements yet.
<% for(let i = 0; i < result.length; i++){ %> 
      <div value="<%= i %>">
    <p><%= result[i].post %></p>
    <input id="<%= i %>" type="submit" value="reply" onclick=bob(event) >
    <input type="submit" value="report" >
      </div>


<form method="post" action="posts"  style="display:none">

<textarea name="replytextarea" id="" cols="30" rows="10"></textarea>
<input type="text" class="forValue" name="theValue" value="5" style="display: none">
<input type="submit" value="submit btw" onclick="setValue(event)">
</form>

<% } %>

I tried trying something with Fetch, but it didn't work properly for me.(I don't know what to write as the URL) 我尝试使用Fetch尝试尝试一些操作,但对我来说却无法正常工作。(我不知道该写什么作为URL)

Cool. 凉。 So one of the neat things about express is that you can make whatever endpoints you want, that will do whatever you want. 因此,关于express的整洁事情之一是,您可以创建所需的任何端点,即可以执行所需的任何操作。 So let's say you want to make an AJAX request that upvotes a certain comment? 假设您要提出一个支持某条评论的AJAX请求? You can create it. 您可以创建它。

(These are PUT routes because they change information about a record that already exists.) (这些是PUT路由,因为它们更改有关已存在记录的信息。)

router.put('/posts/upvote/:comment_id', (req, res) => {
  // something that increments the number of upvotes for that particular comment
  res.status(200).json({voteNum: // new vote number})
})

router.put('/posts/downvote/:comment_id, (req, res) => {
  // something that decrements the number of downvotes for that particular comment
  res.status(200).json({voteNum: // new vote number})
})

And then you can use it: 然后您可以使用它:

// when the upvote button is clicked
fetch(`/posts/upvote/${your_comment_id}`, { method: 'PUT', credentials: 'include' })
  .then(res => res.json())
  .then(jsonRes => {
    // get the element with the number of votes
    element.innerText = jsonRes.voteNum
  })

Of course, this will need some work to fit it into your code. 当然,这需要一些工作才能使其适合您的代码。 But, overall, if you're ever not sure what to write for the URL... just remember, you can make it up. 但是,总的来说,如果您不确定要为该URL写什么...,请记住,可以进行弥补。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不重新加载页面的情况下将值从 controller 发送到 ejs:Nodejs - How to send value from controller to ejs without reloading page : Nodejs 来自 sql 数据库的数据不会在 EJS 页面上呈现,除非它处于循环中(使用 node、express、ejs、mysql) - Data from sql database is not rendering on EJS page unless it is in a loop (using node, express, ejs, mysql) 在向节点发送 ajax 请求而不重新加载页面后更新我在 ejs 中的视图 - Update my view in ejs after sending an ajax request to node without reloading page 如何在ejs中显示数据库中的分层数据 - How to display hierarchical data from my database in ejs 从数据库删除数据而无需重新加载页面 - Delete data from database without reloading the page 从route.js传递变量到我的.ejs页面 - passing a variable from routes.js to my .ejs page 我想在不重新加载页面的情况下从数据库显示div中的数据。 我该怎么做? - I want to show my data in div from database without reloading the page. How I can do this? Express和ejs中的错误,用于从数据库在屏幕上显示数据 - Error in express and ejs, for displaying data on screen from database 我想用 ejs 将数据库中的数据打印到元素中 - I want to print the data from the database into the element with ejs Select 来自数据库的数据取决于节点和 ejs 中的 select 值选项 - Select data from database depends on select value option in node and ejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM