Learn to Code via Tutorials on Repl.it!

← Back to all posts
Setting up Firebase with Nodejs!
h
Squirrel777 (89)

How to use Firebase!

You have no idea how much work was put into this. I literally had to spam repl talk with questions about Firebase lol. Anyways, I am making this tutorial so you wont have to go through everything that I did. Also I think this is the very first Firebase tutorial on replit. So lets get started!

What you need

  • A private repl for security reasons (Optional), do a public one at your own risk, besides who looks at repls!

  • A google account

  • Database and Nodejs experience and Knowledge

What is Firebase?

Before we get started, I just want to refresh your memory about Firebase, if you already know Firebase, skip this part! So what is it? Well, Firebase is one of Googles many Databases that you can use! It's completely free unless you want to upgrade! Its a recently made databases made in 2012, (Its new compared to other databases, mySQL was made in the 1970s!) and yeah that's it. So today you will learn how to set up Firebase!

Getting started and creating a key

So, we have to go to the Firebase console. And do the following:

  • Click Get started

  • Add Project

  • Do everything it says, dont worry about installing anything or writing code!

  • Then Click on the settings at the top left corner

  • Click Project settings

  • Then go to service accounts at the top

  • After that, click Generate new Private key

Coding!

Ok, now at the coding part. So you should also see a code snippet, choose Nodejs, then copy the code you see. Ok, now to replit! So, just create a new node repl, then paste the code in. So then you remember when you had to generate a new private key? And you downloaded a JSON file? Well, upload that file to your repls directory. And rename it key.json. Then paste the code snippet, but replace the path/to/serviceAccountKey.json with ./key.json.

var admin = require("firebase-admin");

var serviceAccount = require("./key.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://<YourProjectId>.firebaseio.com"
});

Ok, almost there! Now write the following code:

const db = admin.firestore();
async function foo() {
const docRef = db.collection('users').doc('alovelace');
await docRef.set({
  first: 'Ada',
  last: 'Lovelace',
  born: 1815
});
}
foo();

Now run your repl! Done, right? No, you should see this error:

it took 4 days to solve this error
But no worries! That means your on the right track! To solve this, Go to the Firebase console again, go to your project, click on Cloud Firestore on the left and enable it! It will only take a few minutes and come back and it will be ready to use. Then Go to your repl, and run it! Hopefully it will raise no errors, the only thing it should return is:

Promise { <pending> }

And to retrieve the data, enter the following code:

const snapshot = await db.collection('users').get();
snapshot.forEach((doc) => {
  console.log(doc.data());
});

And it should return some data! There you have it! You have set up Firebase!

Conclusion

Congratulations! You have set up Firebase! If you are experiencing any errors let me know! Probably because Im terrible at explaining stuff lol. If you want to learn more, I would recommend looking at the docs! Now, why did I say you needed a private repl? Well, because of the following reasons:

  • People can access your database
  • That json file you downloaded, people can access your google account and drive with that! Well, at least according to google.
    So yep, leave an upvote if this tutorial helped you, and remember: If you are experiencing any error's, let me know!

Note: The repl below is private

Commentshotnewtop
Coder100 (12434)

no need for a private repl, you can store stuff inside a .env file too

Squirrel777 (89)

@Coder100 Oh, well I never knew that

EpicGamer007 (1028)

@Squirrel777 .env pog. replit devs were poggers when makin that

JeanBisono (0)

I keep getting a syntax error that says "await is only vaild in async function". This happens when enter in this line of code ==> "const snapshot = await db.collection('users').get();
snapshot.forEach((doc) => {
console.log(doc.data());
}); "
(And no I did not add the parenthesis in my code)

Squirrel777 (89)

@JeanBisono Oh, well it said Database and Nodejs experience and Knowledge required but Here is the correct code:

async function foo() {
const snapshot = await db.collection('users').get();
snapshot.forEach((doc) => {
  console.log(doc.data());
});
}
foo();

It has to be inside of an async function! Hope this helps!

Kookiez (305)

also for the collab (with tsunami and codercode and epicgamer and other people), do you think we should use firebase or repldb?

Squirrel777 (89)

I think repldb, its way easier to set up and much simpler @Kookiez

Squirrel777 (89)

@TsunamiOrSumth I know that, you will know why if you read my tutorial