0

I'm trying to upload multiple photos to my Firebase Storage. For some reason it keeps overwriting the original upload, and it's not creating the folder with the venueID property. Can anybody shine a light here?

main.ts

async localPictureUpload(): Promise<any> {
    // Checks if there is something to be uploaded.
    if (this.photosToUpload.length > 0) {
        const location = `venues/photos/${this.venueID}/`;
        // photosToUpload is an array containing base64 strings.
        this.photosToUpload.forEach(async photoElement => {
            const randomID = this.venueService.createID();
            await this.uploadService.uploadFile(photoElement, location, true)
                .then(async data => {
                    const urlData = await data.ref.getDownloadURL();
                    const photoObject: Photo = {
                        fileName: `${this.venueID}${randomID}`,
                        url: urlData,
                        uploadedBy: this.currentUserID
                    };
                    await this.venueService.addPhoto(this.venueID, photoObject);
                },
                (err) => console.error(err));
        });
    } else {
        return;
    }
}

upload.service

uploadFile(file: any, path: string, base64?: boolean) {
    if (base64) {
        return this.uploadDB.ref(path).putString(file, 'data_url');
    } else {
        return this.uploadDB.upload(path, file);
    }
}
3
  • At first glance the code for setting the path looks fine. Can you try: 1) Adding console.log(path); right before this.uploadDB.ref(path).putString(file, 'data_url') and showing the output? 2) Replacing the complex code with a simpler hard-coded path with / in it, to see if that works. After doing these, please edit your question to show the updated (hopefully simpler) result and output. Commented Mar 12, 2020 at 13:36
  • Hey @FrankvanPuffelen this is what I get on the path console.log venues/photos/0WiFtVRvPj63GlEAqsEF/ that seems ok right? In theory the last part should be the folder (which has the record id) and the file should be added right after, correct? Commented Mar 12, 2020 at 13:48
  • Does my answer helped ? Commented Mar 12, 2020 at 13:50

1 Answer 1

2

The problem is that all the pictures share the same location inside the firesotrage bucket. Because you setup the location path before the forEach.

This code below should create venues/photos/venueID/yourPictures

  // Checks if there is something to be uploaded.
  if (this.photosToUpload.length > 0) {
    // photosToUpload is an array containing base64 strings.
    this.photosToUpload.forEach(async photoElement => {
      const randomID = this.venueService.createID();
      const location = `venues/photos/${this.venueID}/${randomID}/`;
      // const location = `venues/photos/${this.venueID}/`; <---- The problem 
      //  const randomID = this.venueService.createID(); 
      await this.uploadService.uploadFile(photoElement, location, true)
        .then(async data => {
            const urlData = await data.ref.getDownloadURL();
            const photoObject: Photo = {
              fileName: `${this.venueID}${randomID}`,
              url: urlData,
              uploadedBy: this.currentUserID
            };
            await this.venueService.addPhoto(this.venueID, photoObject);
          },
          (err) => console.error(err));
    });
  } else {
    return;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hey thanks, but the problem is that I need to generate a random id for each picture that's being uploaded. If I leave it outside the foreach it will repeat that value for every record.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.