我正在使用Vue.js版本2.5.17,最近使用v-on:change不再有效。
用户单击“选择文件”按钮,并在此更改后应捕获图像名称。此后,它将触发一个或多个文件在屏幕上显示,然后保存到firebase。
相反,我现在遇到错误:
job.vue?d03e:825 Uncaught (in promise) TypeError: Cannot read property 'name' of undefined
at eval (job.vue?d03e:825)
at new Promise (<anonymous>)
at new F (_export.js?90cd:36)
at VueComponent.uploadFile (job.vue?d03e:823)
at VueComponent.uploadProofOfWork (job.vue?d03e:787)
at click (eval at ./node_modules/vue-loader/lib/template-compiler/index.js?{"id":"data-v-45a8035d","hasScoped":false,"optionsId":"0","buble":{"transforms":{}}}!./node_modules/vue-loader/lib/selector.js?type=template&index=0!./src/components/job.vue (0.491509201fd57af656ef.hot-update.js:7), <anonymous>:1096:55)
at invoker (vue.esm.js?efeb:2027)
at HTMLAnchorElement.fn._withTask.fn._withTask (vue.esm.js?efeb:1826)
在连接到v-on:change的fileUploaded
函数中找到name属性。
模板:
<!-- DISPLAY IMAGES -->
<div class="job-images">
<div v-for="(img, index) in this.job.images" :key="index" class="job-image-block">
<img :src="img.url" :alt="img.name" />
</div>
</div>
<!-- REMOVE IMAGE BEFORE UPLOAD -->
<div v-for="(image, index) in images" :key="index">
<a @click.prevent="removeImage(index)">X</a>
<img :src="image.src" />
</div>
<!-- THE CHOOSE IMAGE BUTTON -->
<span class="input-group-text btn btn-primary btn-file" id="basic-addon2">
<input type="file" v-on:change="fileUploaded" accept="image/png, image/jpeg, image/gif"
name="input-file-preview" multiple/>
</span>
<div>
<p>{{ loadingText }}</p>
</div>
<!-- UPLOAD IMAGE(S) BUTTON -->
<vue-button v-userRole.worker="{cb: uploadFile, role: job.role}" accent>
<a @click.prevent="uploadProofOfWork()" style="color: white;">
{{ $t('App.job.uploadFileButton' /* Save Uploaded Images */) }}
</a>
</vue-button>
JS
uploadProofOfWork() {
this.uploadFile().then(imageUrl => {
this.data.image = imageUrl;
db
.collection("jobs")
.where("taskId", "==", this.taskId)
.add(this.data)
.then(function(docRef) {
this.self.clearForm();
this.self.loadingText = this.$t(
"App.job.uploadedPhotoSuccessfully"
) /* Post was created successfully. */ ;
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
});
},
async uploadImages() {
const self = this;
const results = this.images.map(async({ file }) => {
const imageUrl = await this.uploadFile(file, self.job.taskId);
return { name: file.name, url: imageUrl };
});
Promise.all(results).then(async imageUrls => {
if (!Reflect.has(this.job, "images")) this.job.images = [];
const images = [...this.job.images, ...imageUrls];
const result = await db
.collection("jobs")
.doc(this.job.taskId)
.set({ images }, { merge: true })
.then(docRef => {
console.log("updated!", docRef);
});
});
},
uploadFile(file, jobId) {
return new Promise((resolve, reject) => {
const self = this;
const storageRef = firebaseStorage
.ref()
.child("jobs/" + jobId + "/" + file.name + "-" + uuid.v1());
let uploadTask = storageRef.put(file);
uploadTask.on(
"state_changed",
function(snapshot) {
const progress =
snapshot.bytesTransferred / snapshot.totalBytes * 100;
self.loadingText =
this.$t('App.job.uploadedPhotoProgress') /* Upload is */ +
progress +
this.$t(
'App.job.uploadedPhotoProgress2'
);
/* % done. Processing post. */
this.upload.progress = (uploadTask.snapshot.bytesTransferred / uploadTask.snapshot.totalBytes) * 100;
console.log(this.upload.progress);
},
function(error) {
reject(error);
},
async function() {
const downloadUrl = await uploadTask.snapshot.ref.getDownloadURL();
resolve(downloadUrl);
}
);
});
},
async fileUploaded(e) {
const images = await Promise.all(
Array.from(e.target.files).map(file => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = e => {
resolve({ src: e.target.result, file, progress: null });
};
if (e.target.file) {
reader.readAsDataURL(e.target.this.file[0]);
}
});
})
);
this.images = images;
console.log(this.images);
},
答案 0 :(得分:0)
堆栈跟踪显示错误发生在uploadFile
中,由uploadProofOfWork
调用。错误为Cannot read property 'name' of undefined
。 .name
中对uploadFile
的唯一引用是file.name
,其中file
是一个函数参数。如果您查看uploadProofOfWork
,则不会传递任何参数到uploadFile
:
this.uploadFile().then(imageUrl => /* ... */)
看起来您的代码在file
路径的任何地方都没有定义uploadProofOfWork
。我在file
中看到了fileUploaded
,但似乎这些文件打算在uploadImages
中上传(而不是uploadProofOfWork
)。也许您的代码缺少用于“工作量证明”的单独文件输入。