add data to firebase in typescript

To add data to Firebase Realtime Database in TypeScript, first you will need to import the Firebase SDK and initialize it with your Firebase configuration. Then, you can use the set() method to add data to the database.

Here is an example of adding data to the Firebase database using TypeScript:

index.ts
import firebase from 'firebase/app';
import 'firebase/database';

// Initialize Firebase
const firebaseConfig = {
  // Your Firebase configuration
};

firebase.initializeApp(firebaseConfig);

// Get the database reference
const databaseRef = firebase.database().ref();

// Add data to the database
databaseRef.child('users').set({
  name: 'Jane Doe',
  email: 'jane@doe.com',
  age: 30
}).then(() => {
  console.log('Data added to the database.');
}).catch((error) => {
  console.log('Error adding data: ', error);
});
519 chars
24 lines

In this example, we create a reference to the root of the database and add a child node named "users" with the data object containing name, email, and age. The then() block logs a success message to the console, while the catch() block logs an error message if there was an issue adding the data.

Note that you will need to have the necessary write permissions for the database to add data.

gistlibby LogSnag