I'm facing an issue with saving my firebase storage image into a firestore collection variable. Initially, it was working correctly but suddenly stopped functioning, and now the image variable is returning null.
Note: I am using the asia-south1 server.
Below is the template code for ManageProducts:
<div>
<h1>Add Items</h1>
<div>
<form>
<input type="text" placeholder="name" required v-model="item.name" />
<textarea
required
placeholder="description"
v-model="item.description"
></textarea>
<input
type="text"
required
placeholder="price"
v-model="item.price"
/>
<div class="form-group">
<input
type="text"
placeholder="Available/Unavailable"
v-model.lazy="item.status"
class="form-control"
/>
</div>
<div class="form-group">
<input
type="text"
placeholder="Sewing Partner"
v-model.lazy="item.sewingPartner"
class="form-control"
/>
</div>
<input type="file" required @change="uploadImage" accept="image/*" />
<button @click.prevent="AddNewItem">Add Item</button> |
<button class="delete">
Cancel
</button>
</form>
</div>
And here's the script for Manage Products, where all input values are successfully added except for the firebase storage image URL:
<script>
import { dbItemAdd } from "../../main";
import firebase from "firebase";
import "firebase/firestore";
import "firebase/storage";
export default {
name: "AddItems",
components: { AdminPreviewItems },
data() {
return {
items: [],
item: {
name: null,
description: null,
image: null,
price: null,
status: null,
sewingPartner: null,
},
};
},
methods: {
uploadImage(e) {
let file = e.target.files[0];
var storageRef = firebase.storage().ref("products/" + file.name);
let uploadTask = storageRef.put(file);
uploadTask.on(
"state_changed",
(snapshot) => {
console.log(snapshot);
},
(error) => {
// Handle unsuccessful uploads
console.log(error.message);
},
() => {
// Handle successful uploads on complete
// For instance, get the download URL: https://firebasestorage.googleapis.com/...
uploadTask.snapshot.ref.getDownloadURL().then((downloadURL) => {
this.item.image = downloadURL;
console.log("File available at", downloadURL);
});
}
);
},
AddNewItem() {
dbItemAdd
.add({
name: this.item.name,
description: this.item.description,
image: this.item.image,
price: this.item.price,
status: this.item.status,
sewingPartner: this.item.sewingPartner,
})
.then(() => {
location.reload();
console.log("Adding data to Firestore");
})
.catch((error) => {
console.error("Error adding document: ", error);
});
},
},
};
</script>