Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

push data to firebase javascript

<script type="module">
	// Import the functions you need from the SDKs you need
	import { initializeApp } from "https://www.gstatic.com/firebasejs/9.8.0/firebase-app.js";
	import { getFirestore } from "https://www.gstatic.com/firebasejs/9.8.0/firebase-firestore.js";

	// Your web app's Firebase configuration
	const firebaseConfig = {
		apiKey: "API_KEY",
		authDomain: "PROJECT_ID.firebaseapp.com",
		// The value of `databaseURL` depends on the location of the database
		databaseURL: "https://DATABASE_NAME.firebaseio.com",
		projectId: "PROJECT_ID",
		storageBucket: "PROJECT_ID.appspot.com",
		messagingSenderId: "SENDER_ID",
		appId: "APP_ID",
		// For Firebase JavaScript SDK v7.20.0 and later, `measurementId` is an optional field
		measurementId: "G-MEASUREMENT_ID"
	};

	// Initialise Firebase
	const app = initializeApp(firebaseConfig);
	// Initialise Firestore
	const db = getFirestore(app);

	// create a document with a randomly generated id
	async function CreateDocument(collectionName, object) {
		try {
			// add a document to the input collection with the fields in `object`
			return await addDoc(collection(db, collectionName), object);
		} catch (err) {
			console.error("Error writing to database:", err);
			return err;
		}
	}

	async function UpdateDocument(collectionName, documentName, object) {
		try {
			// update a document in the input collection with the input id with the fields in `object`
			await updateDoc(doc(db, collectionName, documentName), object);
		} catch (err) {
			console.error("Error writing to database:", err);
			return err;
		}
	}

	const docId = (await CreateDocument("COLLECTION_NAME", {
		"field-1": 1,
		"field-2": "Hello world",
		"another field": "https://firebase.google.com/docs/database/web/read-and-write"
	})).id;

	await UpdateDocument("COLLECTION_NAME", docId, {
		"field-1": 2,
		"field-3": [ 1, 2, 3, 4 ]
	});
</script>
Comment

PREVIOUS NEXT
Code Example
Javascript :: phaser change background color 
Javascript :: postman response xml json xml2Json 
Javascript :: Capitalize the first letter of string using JavaScript 
Javascript :: this setstate previous state react 
Javascript :: download images from http link in react 
Javascript :: javascript is null 
Javascript :: replace object in array with another array with same id javascript 
Javascript :: node.js child processes 
Javascript :: express post method 
Javascript :: sort array object 
Javascript :: form data object 
Javascript :: css class list 
Javascript :: how to sort an array of object 
Javascript :: datatables get all checkboxes with pagination 
Javascript :: js get day name from date 
Javascript :: javascript convert string with square brackets to array 
Javascript :: png to base64 javascript 
Javascript :: how to submit form on changed url in function in jquery 
Javascript :: live vue js port number 
Javascript :: How to add Strings as numbers in JavaScript 
Javascript :: set input value vanilla js 
Javascript :: how to minimize electron app to tray icon 
Javascript :: bind an event to dom element angular 
Javascript :: for each loop with arrowfunction 
Javascript :: netmask /24 
Javascript :: create an element jquery 
Javascript :: angular material remove outline 
Javascript :: js base64 encoding 
Javascript :: send csrf token ajax laravel 
Javascript :: node get package.json version 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =