我構建了一個評論系統,允許用戶在評論中添加圖像。我試圖等到圖像上傳完成后再向 firestore 添加評論,但我的嘗試不起作用。我有一個名為photoUpload()將圖像上傳到 firebase 存儲的方法。該方法包含一個uploadTask用于獲取進度詳細信息的偵聽器。但是,在圖像上傳完成之前,我的評論已添加到數據庫中。如何延遲并等到完成后再提交評論?這是我的代碼:數據功能: data() { return { text: '', image: null, overlayShow: false, progress: 0, downloadUrl: null } },這是我的圖片上傳任務:photoUpload() { this.filename = uuidv4() const storageRef = this.$fireStorage.ref() this.photoRef = storageRef.child( `photos/${this.userProfile.uid}/commentPhotos/${this.filename}` ) // uploads string data to this reference's location const uploadTask = this.photoRef.putString(this.image, 'data_url') // set the callbacks for each event const next = (uploadTaskSnapshot) => { this.progress = (uploadTaskSnapshot.bytesTransferred / uploadTaskSnapshot.totalBytes) * 100 console.log('Upload is ' + this.progress + '% done') } const error = (error) => { ...snijp... }const complete = async () => { // Upload completed successfully, now we can get the download URL this.downloadUrl = await uploadTask.snapshot.ref.getDownloadURL() } // listens for events on this task uploadTask.on( // 3 callbacks available for each event this.$fireStorageObj.TaskEvent.STATE_CHANGED, { next, error, complete } )}
VueJS 和 Firebase 存儲:如何等到圖像上傳完成后再將 URI 提交到數據庫?
梵蒂岡之花
2023-09-21 17:30:53