如何仅在特定事件之后加载服务器变量

How to load a server variable only after a specific event?

本文关键字:之后 加载 服务器 变量 事件 何仅      更新时间:2023-09-26

我需要两个服务器变量加载后,只有在数据库的var变化。

现在,它们在加载页面时立即加载,因此保持事件(upvotesRef.transaction)之前的值。

 $('.HP').text("<%= post.upvotes - post.downvotes %> HP");

代码:

$('.UpvoteButton').click(function () {

        var $this = $(this);
        if ($this.hasClass("on")) {
            $this.removeClass("on");
            upvotesRef.transaction(function (upvotes) {  
               if (!upvotes) { 
                  upvotes = 0; 
               } 
                 upvotes = upvotes - 1; 
                 return upvotes; 
            }); 
            userRef.remove();
            $('.HP').text("<%= post.upvotes - post.downvotes %> HP");

编辑:

我最终创建了局部变量来解决我的问题。谢谢你的回答!

var upvotesLocal = <%= post.upvotes %>;
var downvotesLocal = <%= post.downvotes %>;
$('.UpvoteButton').click(function () {

        var $this = $(this);
        if ($this.hasClass("on")) {
            $this.removeClass("on");
            upvotesRef.transaction(function (upvotes) {  
               if (!upvotes) { 
                  upvotes = 0; 
               } 
                 upvotes = upvotes - 1; 
                 return upvotes; 
            }); 
            upvotesLocal = upvotesLocal -1
            userRef.remove();
            $('.HP').text((upvotesLocal - downvotesLocal) + " HP")

您应该在事务后获取和更新变量,对吗?
这意味着它应该在回调中发生。
我假设您正在使用firebase事务方法。
然后你的回调应该作为第二个参数:

$('.UpvoteButton').click(function () {

        var $this = $(this);
        if ($this.hasClass("on")) {
            $this.removeClass("on");
            upvotesRef.transaction(function (upvotes) {  
               if (!upvotes) { 
                  upvotes = 0; 
               } 
                 upvotes = upvotes - 1; 
                 return upvotes; 
            }, updateVotes); //<-- here the callback function added
            userRef.remove();
function updateVotes(error, isCommitted, upvotes) {
   //here you got already your new upvotes
   //but you should request the downvotes,
   //here you need the ajax request, see e.g. Dez's answer or comments
}

考虑到我在node.js方面没有经验,你应该做的是将$('.HP').text("<%= post.upvotes - post.downvotes %> HP");行替换为AJAX调用,在那里你可以获得更新的变量并使用jQuery添加它们。下面的内容:

$('.UpvoteButton').click(function () {
        var $this = $(this);
        if ($this.hasClass("on")) {
            $this.removeClass("on");
            upvotesRef.transaction(function (upvotes) {  
               if (!upvotes) { 
                  upvotes = 0; 
               } 
                 upvotes = upvotes - 1; 
                 return upvotes; 
            }); 
            $.ajax({
                type : 'POST',
                data: { userRef: userRef},
                contentType: 'application/json',
                url: 'http://localhost:3000/endpoint',      
                success: function(data) {
                    console.log('success');
                    console.log(JSON.stringify(data));
                    /* Do whatever you need with your data, for example*/
                    $('.HP').text(data.upvotes + "-" + data.downvotes + "HP");
                },
                error : function (event) {
                    console.log(event);
                }
            });
            userRef.remove();
            ...
}

node.js一侧,您将需要以下内容:

app.post('/endpoint', function(req, res) {
  console.log(req.body.userRef)
  /* Get the upvotes and downvotes and send them back in the response */
  res.contentType('json');
  res.send({ some: JSON.stringify({response:'json'}) });
});

我很抱歉不能给出一个准确的答案。