I am encountering a problem within a nextJS app that I developed on Firebase.
In my realtime DB, I have some stored data that I want to read using a component.
Below is my firebase/config.js file:
import {initializeApp} from "firebase/app";
import {getDatabase} from "firebase/database";
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_WEB_API_KEY,
databaseURL: process.env.NEXT_PUBLIC_DATABASE_URL,
projectId: process.env.NEXT_PUBLIC_PROJECT_ID,
};
const firebaseApp = initializeApp(firebaseConfig);
export const firebaseDatabase = getDatabase(firebaseApp);
I used this configuration previously to write data to the DB.
Now, I am working on a component to read the data as shown below:
import {firebaseDatabase} from '../../firebase/config';
// (Changed) import {ref} from "firebase/database";
import {onValue,ref} from "firebase/database";
export default function ShowData() {
const dbRef = ref(firebaseDatabase, 'Restaurant')
console.log('Test onValue')
onValue(dbRef, (snapshot) => {
console.log(snapshot.val())
},
(error) => {
console.log('Error(onValue): '+error);
}
);
return (
<div>
<div>ShowData</div>
{/* ...... */}
</div>
)
} /* End of ShowData */
Even though the error message regarding 'Property 'on' does not exist on type 'DatabaseReference' has disappeared after making code changes,
I am still not getting the expected output displayed.
However, I can verify that there are 8 items under Restaurant in my DB.
Can anyone identify what might be going wrong and suggest a solution?
For testing purposes, I've set my rules as follows:
{
"rules": {
".read": true,
".write": true
}
}