Tips for storing a string or object in a Firebase database

I'm looking for some guidance on how to save a string or object to the Firebase database using JavaScript. I'm currently trying to do this on codepen.io. Here's what I've attempted so far:

// Setting up Firebase app

const firebaseConfig = {
  // Your app configuration goes here
};

firebase.initializeApp(firebaseConfig);

// Accessing the database

const database = firebase.database();

// Adding a string to the database

const myString = "Hello, Firebase!";

database.ref('myData').set(myString);

However, I'm not seeing any new data in the Firebase console. What steps am I missing? Any help would be appreciated.

Answer №1

It appears that the firebaseConfig you provided is empty. Please fill in your Firestore information as shown below:

  const firebaseConfig = {
      apiKey: "YOUR_API_KEY",
      authDomain: "YOUR_AUTH_DOMAIN",
      projectId: "YOUR_PROJECT_ID",
      storageBucket: "YOUR_STORAGE_BUCKET",
      messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
      appId: "YOUR_APP_ID"
    };

I encountered difficulty using it in Codepen, so I created a basic javascript project and tested it with node.js.

If Firebase is not already added to your project, please follow these steps:

npm install firebase

Ensure you import the necessary libraries into your project:

import { initializeApp } from "firebase/app";
import { getFirestore, collection, getDocs } from 'firebase/firestore/lite';

Finally, establish connection to the collection and handle data read/write operations like this:

const testCol = collection(db, 'TestCollection');
const testSnapshot = await getDocs(testCol);
const testList = testSnapshot.docs.map(doc => doc.data());
console.log("Test list",testList);

This resource provides detailed information on setting up Firebase: https://firebase.google.com/docs/web/setup

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can you explain the contrast between img.height and img.style.height?

I'm currently in the process of resizing a series of images by iterating through an array and adjusting their sizes. if(items[0].height > 700 || items[0].width > 700){ items[0].style.height = "700px"; items[0].style.width = "700px"; } As ...

Utilizing JavaScript to manage sections within a dropdown menu

When dealing with this particular HTML code, there is a feature where a list item includes options such as all, a, b, c, and d. If the user selects 'All', it should restrict them from choosing any other items. However, if they do not choose &apos ...

Rendering real-time data using jQuery's Ajax functionality

Imagine having a webpage that gradually returns a large amount of data over time. Here's an example code snippet to illustrate this: <?php $iTime = time(); while(time()-$iTime < 10 ) { echo "Hello world"; echo str_repeat( ' &apos ...

Guide to adding a Json file in a PHP file with PHP

I have a PHP file with an embedded JSON file, and I want to update the JSON file with new information from a form. The form looks like this: <form action="process.php" method="POST"> First name:<br> <input type="text" name="firstName"> ...

What steps do I need to take in order to make the navbar-collapse function properly with an animated

After successfully implementing a collapsible navbar with a hamburger icon for mobile view, I decided to add some extra styling and animation to the hamburger button. However, now the hamburger button remains visible even in desktop view, which is not what ...

Struggling with inputting text in an AngularJS application

I am facing an issue with populating a text input with the output of a function in my HTML code. Despite trying different approaches, I am unable to figure out why it is not working correctly. Below is the snippet of my HTML code: <input type="text ...

Tips for selectively applying CSS to elements within a specific block

How can I apply a specific CSS style only to elements within a certain block? <body> <p>greem green zero</p> <span> hello </span> <div id="main"> ...more tags... </div> <ul><li>1233</li></ul&g ...

Exploring data that is nested within its parent

I'm struggling to understand the concept of Nested selections or how to apply it to my specific situation. Here is an example of the JSON data format I am working with: { 'name': 'root' 'children': [ { &ap ...

How can you create a dynamic bounce effect for text with jquery animate()?

I've been experimenting with Jquery to achieve a bounce effect, here's what I have so far: Html: <div id ="animation">bounce</div> Jquery: $("#animation").animate({ marginTop: "80px" }, 1500 ) .animate({ marginBotto ...

Dropzone.js: Creating a personalized file explorer to include files that have already been uploaded

Don't worry, this isn't your typical "can't load files from the server" query... I'm looking to allow users to view files on the server in a bootstrap modal and then select specific files. After selection, I want to close the modal and ...

Ways to display "No records" message when the filter in the material table in Angular returns no results

How can I implement a "No Records Message" for when the current table is displaying empty data? Check out this link for examples of material tables in AngularJS: https://material.angular.io/components/table/examples ...

When Firebase authentication signs out users who were previously authenticated, it results in them facing permission denied errors

1) User A visits my website, and A is successfully authenticated and can write to the firebase database through the web browser. 2) User B then visits my site, and after being authenticated, they are able to write to the firebase database via the browser. ...

Tips for removing the y-axis line in ChartJs

How can I hide the y axis line in a bubble chart? I attempted to use the code snippet below but it did not work as expected. yAxes: [{ angleLines: { display: false } }] ...

The Express JS route seems to be malfunctioning, as it is returning a 404 error for unknown reasons

I have a link that's supposed to direct me to a page, but every time I click on it, the address changes correctly, yet I end up with a 404 Not Found error. app.js var express = require('express'); var path = require('path'); ...

Transmit the Selected Options from the Checkbox Categories

Here's an intriguing situation for you. I've got a webpage that dynamically generates groups of checkboxes, and their names are unknown until they're created. These groups could be named anything from "type" to "profile", and there's a ...

In Javascript, a function is executed only once within another function that is set on an interval

Using the Selenium Chrome driver in my JavaScript, I am constantly checking a value on a website every 2 seconds. However, I need to only save status changes to a text file, not every single check. The current code is functional but it saves the text fil ...

What is the best way to determine the width of a scroll bar?

Are you familiar with any techniques that work across multiple browsers? ...

Is OnPush Change Detection failing to detect state changes?

Curious about the issue with the OnPush change detection strategy not functioning properly in this demonstration. My understanding is that OnPush change detection should activate when a property reference changes. To ensure this, a new array must be set e ...

Passing an array list back to the parent component in ag-grid(Vue) - A step-by-step guide

Currently, I am integrating AG Grid with Vue. My project has a specific requirement where two checkboxes are displayed using a cellRendererFramework. However, I am facing difficulties in fetching the values of these checkboxes from the row definitions. The ...

Incorporating an AngularJs App into Joomla: A Step-by-

As someone who is currently learning both Angular and Joomla, I am curious about the possibility of integrating an Angular JS Application within Joomla. While Joomla is known for its ease in creating articles and managing content through the admin panel, i ...