如何在Node.js/EJS中单击按钮时触发Firebase POST请求

How to trigger a Firebase POST request when a Button is clicked in Node.js / EJS

本文关键字:按钮 Firebase 请求 POST 单击 Node js EJS      更新时间:2023-09-26

编辑:

实际上仍然存在一些问题。真正的问题实际上是Firebase的安全规则。这里解决了所有问题:如何将Node.js变量放入我的<脚本>lt/脚本>?

问题:

单击"UpvoteButton"或"DownvoteButton"时,如何触发Firebase POST请求?


我尝试了什么:

更新4:

我想我正在进步。在下面查找更新的代码。现在我得到错误:

SyntaxError: Unexpected token ; in ... while compiling ejs

代码:

<% include ../partials/header %>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.5.0/firebase.js"></script>
<script>
<% var config = { %>
<%    apiKey: "info", %>
<%    authDomain: "info", %>
<%    databaseURL: "info", %>
<%    storageBucket: "info", %>
<%    messagingSenderId: "info" %>
<% }; %>
<% firebase.initializeApp(config); %>
</script>
<div class ="containerMarginsDetails">
    <h1 class= "detailsTitle"><%=post.title %></h1>
    <div class="row">
        <img class = "postImg"  src="/images/uploads/<%= post.image %>">
        <span class="UpvoteButton"> </span><span class="DownvoteButton"> </span> <span class="HP"><%= post.upvotes - post.downvotes%> HP</span>
    </div>
</div>
<script> 
    <% var upvotesRef = firebase.database().ref("posts/section/"+id+"/upvotes"); %>
    <% var downvotesRef = firebase.database().ref("posts/section/"+id+"/downvotes"); %>
    $('.UpvoteButton').click(function () {
        <% if(authdata == null) { %>
            window.location.href = "/users/login";
        <% } else { %>
            var $this = $(this);
            var $other = $('.DownvoteButton');
            if ($this.hasClass("on")) {
                $this.removeClass("on");
                <%  upvotesRef.transaction(function (upvotes) { %> 
                <%  if (!upvotes) { %>
                <%    upvotes = 0; %>
                <%   } %>
                <%   upvotes = upvotes - 1; %>
                <%   return upvotes; %>
                <%  }); %>
            } else if (!$this.hasClass('on') && $other.hasClass("on")) {
                $this.addClass('on');
                $other.removeClass("on");
                <%  upvotesRef.transaction(function (upvotes) { %>
                <%   if (!upvotes) { %>
                <%     upvotes = 0; %>
                <%   } %>
                <%   upvotes = upvotes + 1; %>
                <%   return upvotes; %>
                <% }); %>
                <% downvotesRef.transaction(function (downvotes) { %>
                <%  if (!upvotes) { %>
                <%    downvotes = 0; %>
                <%  } %>
                <%  downvotes = downvotes - 1; %>
                <%  return downvotes; %>
                <% }); %>
            } else {
                $this.addClass('on');
                <% upvotesRef.transaction(function (upvotes) { %>
                <%  if (!upvotes) { %>
                <%    upvotes = 0; %>
                <%  } %>
                <%  upvotes = upvotes + 1; %>
                <%  return upvotes; %>
                <% }); %>
            } 
        <% } %>
    });
    $('.DownvoteButton').click(function () {
        <% if(authdata == null) { %>
            window.location.href = "/users/login";
        <% } else { %>
            var $this = $(this);
            var $other = $('.UpvoteButton');
            if ($this.hasClass("on")) {
                $this.removeClass("on");
            } else if (!$this.hasClass('on') && $other.hasClass("on")) {
                $this.addClass('on');
                $other.removeClass("on");
            } else {
                $this.addClass('on');
            }
        <% } %>
    });
</script>
<% include ../partials/footer %>

您必须通过调用firebase.initializeApp(...) 来初始化firebase

就像火球指南告诉我们的那样:

`

// TODO: Replace with your project's customized code snippet
  <script src="https://www.gstatic.com/firebasejs/3.4.0/firebase.js"></script>
  <script>
    // Initialize Firebase
    var config = {
      apiKey: '<your-api-key>',
      authDomain: '<your-auth-domain>',
      databaseURL: '<your-database-url>',
      storageBucket: '<your-storage-bucket>'
    };
    firebase.initializeApp(config);
  </script>`

https://firebase.google.com/docs/web/setup

解决方案是在<script></script>标记之间保持所有代码为纯javascript,并在我的ejs文件中初始化firebase。

问题实际上是关于Firebase安全规则。

在这里找到详细的答案:如何将Node.js变量放入我的<脚本>lt/脚本>?

代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.5.0/firebase.js"></script>
<script>
  // Initialize Firebase
  // TODO: Replace with your project's customized code snippet
  var config = {
    apiKey: "info",
    authDomain: "info",
    databaseURL: "info",
    storageBucket: "info",
    messagingSenderId: "info"
  };
  firebase.initializeApp(config);
</script>
<div class ="containerMarginsDetails">
    <h1 class= "detailsTitle"><%=post.title %></h1>
    <div class="row">
        <img class = "postImg"  src="/images/uploads/<%= post.image %>">
        <span class="UpvoteButton"> </span><span class="DownvoteButton"> </span> <span class="HP"><%= post.upvotes - post.downvotes%> HP</span>
    </div>
</div>
<script>
        var upvotesRef =  firebase.database().ref("posts/fun/<%=id%>/upvotes");
        var downvotesRef = firebase.database().ref("posts/fun/<%=id%>/downvotes");
        $('.UpvoteButton').click(function () {
                      upvotesRef.transaction(function (upvotes) {  
                       if (!upvotes) { 
                          upvotes = 0; 
                       } 
                         upvotes = upvotes - 1; 
                         return upvotes; 
                      }); 
       });
</script>