I've been searching extensively, but I can't seem to make Firebase Realtime Database work for my web app. The goal is to manage products by adding, editing, viewing, and deleting them from a list. Below is the current code snippet from data.js. Any assistance would be greatly appreciated :)
import firebase from 'firebase/compat/app';
import store from "./store";
import 'firebase/compat/auth';
import 'firebase/compat/firestore';
import 'firebase/database';
import { getFirestore } from "firebase/firestore";
import { initializeApp } from "firebase/app";
const firebaseConfig = {
apiKey: "",
authDomain: "",
projectId: "",
databaseURL: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: ""
};
const app = initializeApp(firebaseConfig);
const database = getFirestore();
export default function fetchListings() {
database.ref("/listings")
.get()
.then(function(snapshot) {
if (snapshot.exists()) {
let listings = [];
snapshot.forEach((e) => {
listings.push(e.val());
});
console.log(listings);
store.commit("initListings", listings);
return snapshot.val();
} else {
console.log("No data available");
}
})
.catch(function(error) {
console.error(error);
});
}
export function deleteListing(id) {
firebase
.database()
.ref(`/listings/${id}`)
.remove();
}
/**
* Add/edit listing
* @param {*} listing The listing
*/
export function addListing(listing) {
console.log("ADDING:", listing);
firebase
.database()
.ref(`/listings/${listing.id}`)
.set(listing);
}
export function emptyListing() {
return {
title: "",
price: "",
description: ""
};
}