+ 1
Firebase realtime database issues
How do I display specific user details from firebase realtime database using the users uid using javascript
2 Answers
+ 2
Once you've set up your database on console.firebase.google.com click on the "1 app" button, then the settings icon. Scroll to the bottom of the page and copy your firebase initialization script(make sure npm is selected). The script is usually in this format
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "YOUR-API-KEY",
authDomain: "YOUR-AUTH-DOMAIN",
projectId: "YOUR-PROJECT-ID", storageBucket: "YOUR-STORAGE-BUCKET",
messagingSenderId: "sender id",
appId: "your app id"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
export default getFirestore();
The script above should be stored in a new firebase.js file.
Let's say you have a collection of users named "users" on firebase db. This is how you'll fetch the data
import React from 'react'
import db from 'firebase' // the newly created file...
+ 2
contd...
import { onSnapshot, collection } from 'firebase/firestore'
const [users, setUsers] = React.useState([]);
function getUsers() {
onSnapshot(collection(db, "users"), snapshot => setUsers(snapshot.docs.map(doc => doc.data())));
}
getUsers ();