简体   繁体   English

如何将多个图像上传到 firebase v9 存储并获取所有下载 URL 以使用 Vue.js 在云 Firestore 数据库中更新它

[英]How to upload multiple images to firebase v9 storage and get all download URL 's to update it in cloud firestore database using Vue.js

Vue dev tools screen shot Console log Screen shot Vue 开发工具屏幕截图控制台日志 屏幕截图

Script Code:脚本代码:

 import firebase from "@/firebase";
    import {
      getStorage,
      ref,
      uploadBytes,
      getDownloadURL,
      updateDoc,
    } from "firebase/storage";
    export default {
      data() {
        return {
          imagesUrl: [],
        };
      },
      methods: {
        chooseFile() {
          document.getElementById("imgUpload").click();
        },
    
        uploadImage(e) {
          this.files.push(e.target.files[0]);
          this.images.push(URL.createObjetURL(this.files));
        },
        createCity: async function () {
          const docRef = firebase.addDoc(
            firebase.collection(firebase.firestoreDB, "cities"),
            {
              name: "Tokyo",
              country: "Japan",
            }
          );
    
          var photos = [];
    
          for (var i = 0; i < this.files.length; i++) {
            // files.values contains all the files objects
            const file = this.files[i];
            const storage = getStorage();
            const metadata = {
              contentType: "image/jpeg",
            };
            const storageRef = ref(storage, "temp/" + docRef.id + "/" + file.name);
    
            const snapshot = await uploadBytes(storageRef, file, metadata);
            const downloadURL = await getDownloadURL(snapshot.ref);
            photos.push(downloadURL);
          }
          await console.log(photos);
          await updateDoc(docRef.id, { photosURLs: photos });
        },
      },
    };

Console Log Data:控制台日志数据:

  • TypeError: Object(...) is not a function at VueComponent._callee$ (fragment.vue?262c:453:1) at tryCatch (runtime.js?96cf:63:1) at Generator.invoke [as _invoke] (runtime.js?96cf:294:1) at Generator.eval [as next] (runtime.js?96cf:119:1) at asyncGeneratorStep (asyncToGenerator.js?1da1:3:1) at _next (asyncToGenerator.js?1da1:25:1) TypeError: Object(...) is not a function at VueComponent._callee$ (fragment.vue?262c:453:1) at tryCatch (runtime.js?96cf:63:1) at Generator.invoke [as _invoke] ( runtime.js?96cf:294:1) at Generator.eval [as next] (runtime.js?96cf:119:1) at asyncGeneratorStep (asyncToGenerator.js?1da1:3:1) at _next (asyncToGenerator.js?1da1 :25:1)

You should not use the await keyword within a for or a forEach loop, see here or here .您不应在forforEach循环中使用await关键字,请参见此处此处

You should use Promise.all() as follows (untested):您应该使用Promise.all()如下(未经测试):

  const promises = [];

  for (var i = 0; i < this.files.length; i++) {
    // files.values contains all the files objects
    const file = this.files[i];
    const storage = getStorage();
    const metadata = {
      contentType: "image/jpeg",
    };
    const storageRef = ref(storage, "temp/" + docRef.id + "/" + file.name);

    promises.push(uploadBytes(storageRef, file, metadata).then(uploadResult => {return getDownloadURL(uploadResult.ref)}))
    
  }

  const photos = await Promise.all(promises);

  await console.log(photos);
  await updateDoc(docRef, { photosURLs: photos }); // <= See the change here docRef and not docRef.id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 reactjs 在 firebase v9 云存储中上传多张图片 - How can I upload multiple images in firebase v9 cloud storage using reactjs 在 React.js 中使用 Dropzone 时如何将图像上传到 Firebase 存储并获取图像的下载 URL - How to Upload Images to Firebase Storage and Get Image's Download URL's When Using Dropzone in React.js 在 Firebase Firestore 中查询整个集合时如何获取对象属性的子集? 使用JS客户端sdk v9 - How to get a subset of object's properties when querying an entire collection in Firebase Firestore? Using JS client sdk v9 使用 Firebase v9 将文件从网站上传到 Firebase 存储 - File upload from website to Firebase storage using Firebase v9 将多个图像上传到 Firebase 存储,然后将下载 URL 调用到 Firebase 数据库 - Uploading Multiple Images to Firebase Storage, and Then Calling the Download URL's to Firebase Database 如何从 firebase 存储中获取图像 URL 列表并将其上传到云 firestore? - How to get a list of image URL from firebase storage and upload it to cloud firestore? 使用 Vue.js 在 Laravel 中上传多张图片 - Upload Multiple images in Laravel using Vue.js 列出 firebase storage v9 中的所有文件 - list all files in firebase storage v9 如何使用 Vue.js 下载图像而不显示它们? - How to download images without displaying them using Vue.js? JS和Firebase。 上传多个图像,等待URL并上传到数据库 - JS and Firebase. Upload multiple images, Wait for URL and upload to database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM