gpt4 book ai didi

javascript - 如何使用 Vue.js 组件从 Firestore 中删除图像

转载 作者:行者123 更新时间:2023-12-02 23:10:50 26 4
gpt4 key购买 nike

我正在尝试重新利用我之前找到的 Vue.js 图像 uploader 组件 ( https://codesandbox.io/s/219m6n7z3j )。该组件支持将图像上传到 Firebase 存储,以及将 downloadURL 保存到 firestore,以便图像即使在刷新后也能持续呈现在屏幕上。我一直在尝试向组件添加 deleteImg() 方法,但收到错误:

FirebaseError: Function CollectionReference.doc() requires its first argument to be of type non-empty string, but it was: undefined

有关如何设置 deleteImg() 方法以便从存储和数据库中删除图像的任何建议?

<template>
<div id="app">
<v-toolbar color="indigo" dark fixed app>
<v-toolbar-title>Vue Firebase Image Upload</v-toolbar-title>
</v-toolbar>
<v-app id="inspire">
<v-content>
<v-container fluid>
<v-layout align-center justify-center>
<v-flex xs12 sm8 md4>
<img :src="imageUrl" height="150" v-if="imageUrl" />
<v-text-field label="Select Image" @click="pickFile" v-model="imageName"></v-text-field>
<input type="file" style="display: none" ref="image" accept="image/*" @change="onFilePicked"/>
<v-btn color="primary" @click="upload">UPLOAD</v-btn>
</v-flex>
</v-layout>
<br />

<v-layout align-center justify-center>
<v-flex xs12 sm8 md4>
<div v-for="img in imgUrls" :key="img.id">
<br />
<img :src="img.downloadUrl" height="150" />
<v-btn @click="deleteImg(img.id)">x</v-btn>
</div>
</v-flex>
</v-layout>
</v-container>
</v-content>
</v-app>
</div>
</template>

<script>
import firebase from 'firebase'
import { db } from "./main";

export default {
name: "App",
data() {
return {
photo: null,
photo_url: null,
dialog: false,
imageName: "",
imageUrl: "",
imageFile: "",
imgUrls: []
};
},
created() {
this.getImages();
},
methods: {
getImages: function() {
db.collection("images")
.get()
.then(snap => {
const array = [];
snap.forEach(doc => {
array.push(doc.data());
});
this.imgUrls = array;
});
this.imageName = "";
this.imageFile = "";
this.imageUrl = "";
},

pickFile() {
this.$refs.image.click();
},

onFilePicked(e) {
const files = e.target.files;
if (files[0] !== undefined) {
this.imageName = files[0].name;
if (this.imageName.lastIndexOf(".") <= 0) {
return;
}
const fr = new FileReader();
fr.readAsDataURL(files[0]);
fr.addEventListener("load", () => {
this.imageUrl = fr.result;
this.imageFile = files[0]; // this is an image file that can be sent to server...
});
} else {
this.imageName = "";
this.imageFile = "";
this.imageUrl = "";
}
},

upload: function() {
var storageRef = firebase.storage().ref();
var mountainsRef = storageRef.child(`images/${this.imageName}`);
mountainsRef.put(this.imageFile).then(snapshot => {
snapshot.ref.getDownloadURL().then(downloadURL => {
this.imageUrl = downloadURL;
const bucketName = "xxx-xxxx-xxxxx.xxxxxxx.xxx";
const filePath = this.imageName;
db.collection("images").add({
downloadURL,
downloadUrl:
`https://firebasestorage.googleapis.com/v0/b/${bucketName}/o/images` +
"%2F" +
`${encodeURIComponent(filePath)}?alt=media`,
timestamp: Date.now()
});
this.getImages();
});
});
},
deleteImg(img) {
db.collection("images").doc(img).delete()
.then(() => {
console.log('Document successfully deleted')
})
.then(() => {
this.getImages()
})
}
},
components: {}
};
</script>

<style>
#app {
font-family: "Avenir", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

最佳答案

我猜doc.data()中没有可用的id,需要手动添加id

getImages: function() {
db.collection("images")
.get()
.then(snap => {
const array = [];
snap.forEach(doc => {
const data = doc.data()
array.push({
id: doc.id,
...data
});
});
this.imgUrls = array;
});
this.imageName = ""
this.imageFile = ""
this.imageUrl = ""
},

关于javascript - 如何使用 Vue.js 组件从 Firestore 中删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57367935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com