I'm trying to display posts from Firestore using Firebase, and I updated my firebase.js to use the latest modular SDK. However, I'm still encountering an error on localhost that says:
error:
error message on local host
The firebase.js file is as follows:
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getFirestore } from "firebase/firestore";
const firebaseConfig = {
apiKey: "###",
authDomain: "###",
projectId: "###",
storageBucket: "###",
messagingSenderId: "###",
appId: "###"
};
const firebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(firebaseApp);
const auth = getAuth();
export { db, auth };
And my Feed.js file contains:
import React, { useEffect, useState } from 'react'
import "./Feed.css"
import CreateIcon from '@mui/icons-material/Create'
import InputOption from './InputOption'
import ImageIcon from '@mui/icons-material/Image'
import SubscriptionsIcon from '@mui/icons-material/Subscriptions'
import EventNoteIcon from '@mui/icons-material/EventNote'
import CalendarViewDayIcon from '@mui/icons-material/CalendarViewDay'
import Post from './Post'
import { db } from './firebase'
import firebase from 'firebase/compat/app'
import 'firebase/compat/auth'
import 'firebase/compat/firestore'
function Feed() {
const [input, setInput] = useState("")
const [posts, setPosts] = useState([])
useEffect(() => {
db.collection("posts").onSnapshot(snapshot => (
setPosts(snapshot.docs.map((doc) => (
{
id: doc.id,
data: doc.data(),
}
))
))
}, [])
const sendPost = e => {
e.preventDefault()
db.collection("posts").add({
name: "Luke Knight",
description: "this is a test",
message: input,
photoUrl: "",
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
})
}
return (
<div className='feed'>
<div className='feed__inputContainer'>
<div className='feed__input'>
<CreateIcon />
<form>
<input value={input}
onChange={e => setInput(e.target.value)}
type="text"/>
<button onClick={sendPost} type="submit">Send</button>
</form>
</div>
<div className='feed__inputOptions'>
<InputOption Icon={ImageIcon} title="Photo" color="#70B5F9"/>
<InputOption Icon={SubscriptionsIcon} title="Video" color="#E7A33E"/>
<InputOption Icon={EventNoteIcon} title="Event" color="#C0CBCD"/>
<InputOption Icon={CalendarViewDayIcon} title="Write article" color="#7FC15E"/>
</div>
</div>
{posts.map(({id, data: { name, description, message, photoUrl} }) => (
< Post
key={id}
name={name}
description={description}
message={message}
photoUrl={photoUrl}
/>
))}
<Post name="Luke Knight" description="This is a test" message="This worked" />
</div>
)
}
export default Feed;
I believe my firebase.js code is correct but the Feed.js file is still using the older namespaced syntax (like db.collection) instead of the modular functions. I've tried following several Stack Overflow answers and updating to the latest SDK, yet I continue to see the error. How can I resolve this issue and correctly integrate Firestore in my React component?