I'm unable to save my firebase storage image into a firestore collection variable. It was working fine for some time but then stopped working. The image variable returns null.
ps - I'm using the asia-south1 server
Here's my 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>
Here's the script for Manage Products. In here, I'm able to add all the other input values except for the firebase storage's 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>
When are you calling the AddNewItem
function? If the image is not uploaded yet or the downloadURL is not fetched, if you click the submit button it'll be null.
uploadTask.snapshot.ref.getDownloadURL().then((downloadURL) => {
this.item.image = downloadURL;
console.log("File available at", downloadURL);
//function must be called after this
});
You can disable the submit button is this.item.image
is null (default value).
<button :disabled="!item.image" @click.prevent="AddNewItem">Add Item</button>
I don't see state_changed
observer being used. Do you need to show the upload progress? If yes, keep the existing code as it is else I would refactor it as an async function like this:
async uploadImage(e) {
let file = e.target.files[0];
if (!file) alert("No file selected")
var storageRef = firebase.storage().ref("products/" + file.name);
let uploadTask = await storageRef.put(file);
const url = await storageRef.getDownloadURL()
this.item.image = url
}
You are actually uploading the image on file change and not submit button click so you could add the Firestore document from the @change
event itself right after getting downloadURL.
Once you upload a file to storage, you call the getDownloadURL()
which will generate an access token
You then store this URL and access token directly into firestore
// Create reference to image
var mountainImagesRef = storageRef.child('images/mountains.jpg');
// Put image file to Storage
ref.put(file).then((snapshot) => {
// On complete, get the downloadURL
return mountainImagesRef.getDownloadURL()})
.then((downloadURL) =>
// Add download URL to Firestore
firestore().doc("path/to/document/here").add({
name: this.item.name,
description: this.item.description,
image: downloadURL,
price: this.item.price,
status: this.item.status,
sewingPartner: this.item.sewingPartner,})
).catch(console.error);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.