How To Trigger A Firebase Post Request When A Button Is Clicked In Node.js / Ejs
EDIT: There were actually still some issues. The real problem was actually the Firebase security rules. All was solved here : How to put a Node.js variable inside my
Solution 1:
You have to initialize firebase by calling firebase.initializeApp(...)
Just like firebase guides tell us to:
`
// TODO: Replace with your project's customized code snippet
<scriptsrc="https://www.gstatic.com/firebasejs/3.4.0/firebase.js"></script><script>// Initialize Firebasevar config = {
apiKey: '<your-api-key>',
authDomain: '<your-auth-domain>',
databaseURL: '<your-database-url>',
storageBucket: '<your-storage-bucket>'
};
firebase.initializeApp(config);
</script>`
Solution 2:
The Solution was to keep all the code in plain javascript between the <script></script>
tags and initialise firebase also in my ejs file.
The problem was really about Firebase security rules.
Find the detailed answer here: How to put a Node.js variable inside my <script></script>?
CODE:
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://www.gstatic.com/firebasejs/3.5.0/firebase.js"></script><script>// Initialize Firebase// TODO: Replace with your project's customized code snippetvar config = {
apiKey: "info",
authDomain: "info",
databaseURL: "info",
storageBucket: "info",
messagingSenderId: "info"
};
firebase.initializeApp(config);
</script><divclass ="containerMarginsDetails"><h1class= "detailsTitle"><%=post.title %></h1><divclass="row"><imgclass = "postImg"src="/images/uploads/<%= post.image %>"><spanclass="UpvoteButton"></span><spanclass="DownvoteButton"></span><spanclass="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>
Post a Comment for "How To Trigger A Firebase Post Request When A Button Is Clicked In Node.js / Ejs"