I'm new to firebase storage. I've been able to use firestore with documents using VUE, but I'm using storage for images.
I'm getting continual errors and I believe it relates to failing to create the reference as required.
Questions:
Here is the config setup:
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { getStorage, ref } from "firebase/storage";
const firebaseConfig = {
[Info]
};
// init firebase
initializeApp(firebaseConfig);
// init firestore service
const db = getFirestore();
// Get a reference to the storage service, which is used to
create references in your storage bucket
const storage = getStorage(initializeApp(firebaseConfig));
const storageRef = ref(storage);
export { db, storage, storageRef };
Here is the Vue setup:
import { computed, ref } from "vue";
//firebase imports
import { db, storage, storageRef } from
"../../firebase/config";
import { collection, getDocs } from "firebase/firestore";
import { getDownloadURL } from "firebase/storage";
export default {
name: "Name",
setup() {
const imagesRef = storageRef(storage);
getDownloadURL(imagesRef).then((url) => {
// Insert url into an <img> tag to "download"
console.log(url);
});
The following will do the trick:
Firebase config:
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { getStorage } from "firebase/storage";
const firebaseConfig = {
apiKey: "...",
// ...
};
const firebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(firebaseApp);
const storage = getStorage(firebaseApp);
export { db, storage };
Vue.js component:
// ...
<script>
import { db, storage } from '../../firebase/config'; // Import db only if you need to use Firestore in this component
import {
collection,
// ...
} from 'firebase/firestore'; // Idem, only if you need to use Firestore in this component
import {
ref,
getDownloadURL,
// ...
} from 'firebase/storage';
export default {
// ...
data: () => ({
// ...
}),
methods: {
async uploadFile(file) {
try {
const fileName = file.name;
const fileRef = ref(storage, `myFolder/${fileName}`);
// ....
You may use an alias when importing the ref function required by Firebase Storage and distinguish it from the required by Vue.
import Vue from "vue";
import App from "./App.vue";
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { getStorage, ref as refStorage } from "firebase/storage";
const firebaseConfig = {
[Info]
};
// init firebase
const firebaseApp = initializeApp(firebaseConfig);
// init firestore service
const db = getFirestore();
const storage = getStorage(firebaseApp);
// Get a reference to the storage service, which is used to create references in your storage bucket
const storageRef = refStorage(storage);
export { db, storage, storageRef };
new Vue({
render: (h) => h(App)
}).$mount("#app");
I have used Code Sandbox to test the above code and added the following versions of Firebase dependencies to the package.json file:
"@firebase/storage": "0.9.0"
"firebase": "9.6.1"