【问题标题】:How to trigger a Firebase POST request when a Button is clicked in Node.js / EJS在 Node.js / EJS 中单击按钮时如何触发 Firebase POST 请求
【发布时间】:2017-03-06 12:21:21
【问题描述】:

编辑:

实际上还是有一些问题。真正的问题实际上是 Firebase 安全规则。一切都在这里解决了:How to put a Node.js variable inside my <script></script>?

问题:

点击“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浏览器sdk吗?它会在没有明确的 http 请求的情况下保存您的数据。 firebase.google.com/docs/web/setup
  • @VladimirGabrielyan 这就是我正在使用的(我认为)。我不确定我明白你的意思:)
  • 说触发 Firebase Post 请求是指保存或更新数据库中的某些内容?
  • 我的意思是保存和更新。在所需路径设置新数据。
  • 好吧,使用 firebase sdk 执行所有操作,使用我上面提到的链接。使用 firebase sdk,您可以对 firebase 数据库进行所有操作(创建、读取、更新删除)

标签: javascript jquery firebase firebase-realtime-database


【解决方案1】:

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

就像 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

【讨论】:

  • 不,没有改变任何东西。无论如何都要初始化两次firebase会很奇怪,但是感谢您的帮助:)投票。
【解决方案2】:

解决方案是将所有代码保留在 &lt;script&gt;&lt;/script&gt; 标签之间的纯 javascript 中,并在我的 ejs 文件中初始化 firebase。

问题实际上与 Firebase 安全规则有关。

在这里找到详细答案:How to put a Node.js variable inside my <script></script>?

代码:

<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>

【讨论】:

    猜你喜欢
    • 2019-04-12
    • 2019-12-10
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 2018-05-11
    相关资源
    最近更新 更多