EDIT:
实际上仍然存在一些问题。真正的问题实际上是 Firebase 安全规则。一切都在这里解决了:如何将 Node.js 变量放入我的 中?
问题:
单击“Downvote Button”的“Upvote Button”时如何触发Firebase POST请求?
我尝试过的:
更新4:
我想我正在进步。在下面找到更新的代码。现在我收到错误:
SyntaxError: Unexpected token ; in ... while compiling ejs
CODE:
<% 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 %>
您必须通过调用来初始化 firebasefirebase.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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)