繁体   English   中英

如何将Node.js变量放入我的 <script></script> ?

[英]How to put a Node.js variable inside my <script></script>?

问题:

我在脚本标签中有一些Firebase代码,但未执行。 我应该如何在其中写入<%= id%>变量?

通常,当我单击按钮时,应该触发一个请求以更新Firebase中的upvote值,但是什么也不会发生。

该代码未执行,但没有错误。


码:

file.ejs

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

这是路由器文件。

file.js

var express = require("express");
var router = express.Router();

var firebase = require("firebase");

router.get('/details/:id', function(req, res){
    global.page_name = "fun";
    var id = req.params.id;
    var funPostRef = firebase.database().ref("posts/fun/"+id);

    funPostRef.once('value', function(snapshot){
        var funPost = snapshot.val();
        res.render('fun/details', {post: funPost, id:id});
    });
});

module.exports = router;

注意:Firebase也已在我的app.js文件中初始化。

编辑:

如果尝试使用此版本的代码,则每次我重新加载页面时都会触发firebase请求,而不是单击时触发。 如何确保仅在单击时才触发它?

<% 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; 
      }); %>
   });

编辑2:

发现错误! 这是什么意思 ?

在此处输入图片说明

这很奇怪,因为当我单击按钮时已通过身份验证,这是我的安全规则:

{
  "rules": {
    ".read": "true",
    ".write": "auth != null"
  }
}

编辑3:

我还收到以下(非崩溃)错误消息:

不赞成在主线程上使用同步XMLHttpRequest,因为它会对最终用户的体验产生不利影响。 如需更多帮助,请访问https://xhr.spec.whatwg.org/

编辑4:

如果我将规则更改为:

{
  "rules": {
    ".read": "true",
    ".write": "true"
  }
}

该按钮可以正常工作!

但是,尽管我已通过身份验证,但代码为何与以前的规则不兼容?

由于您使用的是模板语言,因此不需要+运算符:

var upvotesRef =  firebase.database().ref("posts/fun/<%=id%>/upvotes");

您可以尝试使用<%-id%>“ echo” id

可悲的是,我无法发表评论(代表人数不足),否则如果您检查按钮,我会问您是否看到ID。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM