How can I get user displaynames who posted comments with Firestore v9 in React
firebase.js
import { initializeApp } from 'firebase/app';
import { getFirestore } from "firebase/firestore";
// TODO: Replace the following with your app's Firebase project configuration
const firebaseConfig = {
apiKey: "",
authDomain: "",
projectId: "",
databaseUrl: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: ""
};
const app = initializeApp(firebaseConfig);
export const db = getFirestore(app);
user.js
import { db } from "./firebase";
import { collection, addDoc, getDocs } from "firebase/firestore";
export const getuser = async () => {
try {
const querySnapshot = await getDocs(collection(db, "user"))
return querySnapshot
}
catch (e) {
console.error("Error adding document: ", e);
}
}
import React, { useEffect, useState } from 'react'
import { getuser } from './user.js'
export default function Home() {
const [user, setUser] = useState([])
useEffect(() => {
let data = await getuser()
let users = []
data.forEach(doc => {
users.push(doc.data())
})
setUser(users)
}, []);
return (
<div>
{user.map((item, index) => (
<div>
<p>{item.displayName}</p>
<p>{item.idea}</p>
</div>
))}
</div>)
}