Empurre dados para o javascript de Firebase
<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>
MattDESTROYER