【问题标题】:How to put a Node.js variable inside my <script></script>?如何将 Node.js 变量放入我的 <script></script> 中?
【发布时间】:2017-03-17 11:27:07
【问题描述】:

问题:

我在脚本标签中有一些 Firebase 代码,但它没有执行。我应该如何在其中写入我的 变量?

通常,当我单击按钮时,应触发更新 Firebase 中的赞成值的请求,但没有任何反应。

代码没有执行,但我没有收到任何错误。


代码:

文件.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>

这是路由器文件。

文件.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"
  }
}

按钮效果很好!

但是为什么我通过了身份验证,代码却不能使用前面的规则???

【问题讨论】:

    标签: javascript jquery node.js firebase


    【解决方案1】:

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

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

    【讨论】:

    • 我试过了。遗憾的是它并没有改变任何东西:/(这就是为什么我尝试了通常用于字符串连接的“”+var+“”符号)。
    • 令人困惑的是我没有收到任何错误消息:(
    • 我建议查看模板语言生成的源代码并确保它看起来有效。然后我会在各处添加 console.log 语句,以查看哪些代码被调用,哪些代码未被调用。
    • 我查看了生成的源代码,一切正常。至于console.logs,除非我这样写,否则它们不会打印任何东西:&lt;% console.log(something) %&gt; Btw:我用我尝试过的代码执行的地方更新了我的问题,但后来我被困住了,无法只执行它单击时(Jquery 是客户端,firebase 函数是服务器端)。你会推荐做什么?似乎只有&lt;% %&gt; 之间的代码正在执行。
    • 我能够执行 console.logs 并发现错误 >:) 我用屏幕截图更新了我的问题。错误是什么意思?
    【解决方案2】:

    您可以尝试使用

    “回显” id

    遗憾的是,我无法发表评论(没有足够的代表),否则我会问如果您检查按钮是否看到 id。

    【讨论】:

    • 我试过你的表示法,但我一定做错了什么,因为我的终端没有任何关于 id 的打印。我还必须指出,我试图放在脚本标签之间的所有 console.log 语句都不会打印到我的终端。
    • 不过,我可以通过编写 来打印 id。它就在那里。
    • 但是 console.log 打印在服务器端对吗?
    • 如果你把它写进一个脚本标签和
    • 当我写代码时,我的终端(服务器端)会打印出 id:
    猜你喜欢
    • 1970-01-01
    • 2018-01-28
    • 2021-01-15
    • 2016-07-16
    • 1970-01-01
    • 2015-04-22
    • 2016-10-13
    • 2011-08-29
    • 2015-12-31
    相关资源
    最近更新 更多