0

I've recently gotten into using Firebase to host my website. And I'd like to display some stats which are fetched from a database call using Google Cloud Functions whenever the page is loaded by a user.

However, I'm unsure how I can insert this data into a tag on my website.

    <div class="stats">
        <div class="usersCard">
            <h4 id=users>Users</h4>
            <h1 id="usersVal">INSERTSTATSHERE</h1>
        </div>
        <div class="serversCard">
            <h4 id=Servers>Servers</h4>
            <h1 id="serversVal">INSERTSTATSHERE</h1>
        </div>
        <div class="infectionsCard">
            <h4 id=infections>Infections</h4>
            <h1 id="infectionsVal">INSERTSTATSHERE</h1>
        </div>
const functions = require('firebase-functions');

exports.bigben = functions.https.onRequest((req, res) => {
    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&ssl=false";

    MongoClient.connect(url, function(err, db) {
        if (err) throw err;
        var dbo = db.db("discord_pandemic");
        dbo.collection("ArrayStats").find().toArray(function(err, result) {
            if (err) throw err;
            console.log(result)
            const totalInfections = result[0].total_infections
            const totalUsers = result[0].total_users
            const totalCommands = result[0].total_commands
            const totalLiveInfections = result[0].total_live_infections
            const totalLiveInfectedChannels = result[0].total_live_infected_channels

        });
    });
});

As seen above, I have all the code ready. I just do not know how to edit an attribute on my webpage from a Google Cloud Function. Any help would be very appreciated.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
deeform
  • 25
  • 4
  • Are you asking how to [call Cloud Functions from within your web app?](https://firebase.google.com/docs/functions/callable) – Frank van Puffelen Aug 12 '21 at 20:44
  • I'm trying to update a

    tag on `pageload` using exports.app = functions.https.onRequest(app); But I can't seem to figure out how I can edit specific elements of a web page when using firebase. I usually use ```document.getElementById("lol").innerHTML = "lolsss")``` to edit specific elements. But I get a `document is undefined` error.

    – deeform Aug 12 '21 at 21:33
  • That's because the code inside `functions.https.onRequest((req, res) => {` runs on the server, so there is no `document` available there. You'll need to call that code from your web app, and then use the response in that web client to update its HTML. – Frank van Puffelen Aug 12 '21 at 22:03
  • Interesting. I am currently attempting this now. Thank you :) – deeform Aug 12 '21 at 22:23
  • If you have to put your `firebase config` inside of your – deeform Aug 12 '21 at 22:41
  • Search is your friend [here](https://www.google.com/search?q=If+you+have+to+put+your+firebase+config+inside+of+your+%3Cscript%3E+tag+in+your+HTML+page%2C+it+contains+sensitive+information%2C+such+as+your+APIKEY.+Doesn%27t+this+make+it+vulnerable+to+abuse%3F), as it leads to https://stackoverflow.com/questions/37482366/is-it-safe-to-expose-firebase-apikey-to-the-public – Frank van Puffelen Aug 13 '21 at 01:25
  • Thanks for linking that. Very useful to know :) – deeform Aug 13 '21 at 01:28

0 Answers0