gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:01:41 25 4
gpt4 key购买 nike

编辑:

其实还是有一些问题。真正的问题实际上是 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.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

关于javascript - 在 Node.js/EJS 中单击按钮时如何触发 Firebase POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40223230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com