简体   繁体   English

Angular Error not allowing me to update and upload images in my Firebase Storage 找不到存储桶的原因

[英]Angular Error not allowing me to update and upload images in my Firebase Storage cause of not finding storage bucket

Good day developers, im building this simple app of uploading images to a gallery of images in angular, but im having issues with uploading the images i got in my uploading form to the Firebase Storage throwing me an error referring to a not defined storageBucket, which is alreday declared in my environment variables, both in the folder environment.ts and environment.prod.ts, like this:开发者好,我正在构建这个简单的应用程序,将图像上传到 angular 中的图像库,但是我在将我在上传表单中获得的图像上传到 Firebase 存储时遇到问题,并向我抛出一个错误,指的是未定义的 storageBucket,它已经在我的环境变量中声明了,在文件夹 environment.ts 和 environment.prod.ts 中,如下所示:

ENVIRONMENT FOLDERS

export const environment = {
  production: false,
  firebaseConfig : {
    apiKey: "----------------------------------------",
  authDomain: "images-gallery-abbac.firebaseapp.com",
  databaseURL: "https://images-gallery-abbac.firebaseio.com",
  projectId: "images-gallery-abbac",
  storageBucket: "images-gallery-abbac.appspot.com",
  messagingSenderId: "357163460358",
  appId: "-----------------------------------------"
  }
};

on my ts component where my method is, i did this as imports, constructors and methods:在我的方法所在的 ts 组件上,我将其作为导入、构造函数和方法来执行:

IMPORTS AND CONSTRUCTORS

import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { AngularFireStorage } from '@angular/fire/storage';
import { finalize } from 'rxjs/operators';
@Component({
  selector: 'app-image',
  templateUrl: './image.component.html',
  styleUrls: ['./image.component.css'],
})
export class ImageComponent implements OnInit {

  imgSrc: string = '/assets/img/default.png';
  selectedImg: any = null;
  isSubmitted: boolean;

  formTemplate = new FormGroup({
    caption: new FormControl('', Validators.required),
    category: new FormControl(''),
    imageUrl: new FormControl('', Validators.required),
  });

  constructor(private fireStorage: AngularFireStorage) {}

Basically importing all referring to Firebase Storage, also initializing 3 variables referring to the imgage the user select by the moment, as well as a img by default, and a boolean which keeps an eye on the submission or not of the form.In the constructor,i call the AngularFireStorage through the initialized variable called fireStorage.基本上导入所有引用 Firebase 存储,还初始化 3 个变量,引用当前用户 select 的图像,以及默认的 img,以及一个 boolean,它关注是否提交表单。在构造函数中,我通过名为 fireStorage 的初始化变量调用 AngularFireStorage。

METHOD FOR UPLOADING IMAGES

   onSubmit(formValue) {

    if (this.formTemplate.valid) {
      var filePath = `${formValue.category}/${this.selectedImg.name}`;
      const fileRef = this.fireStorage.ref(filePath);
      this.fireStorage
        .upload(filePath, this.selectedImg)
        .snapshotChanges()
        .pipe(
          finalize(() => {
            fileRef.getDownloadURL().subscribe((url) => {
              formValue['imageUrl'] = url;
            });
          })
        ).subscribe();
    }
  }

Thus basically under the condition the form to upload images is true i declare a variable (filePath)which depict the path in my storage to allow the selected image,(also a second variable is declared(fileRef)in charge of allowing the reference or path where the image selected is storaged in the storage of Firebase )then accessing the service of AngularFireStorage declared to a variable initialized in the constructor i commit the method upload(), passing as parameters the path i want to establish in my storage as well as the image selected by user and allowed in variable selectedImg, and then through the method snapshotChanges,which returns and Observable, and using the other method pipe() i include finalize(), which basically emulates with the observable thrown by snapshotChanges() executing a final function when this method's flow ends, in this case accessing through the varaible fileRef to the method getDownloadUrl因此,基本上在上传图像的形式为真的情况下,我声明了一个变量(filePath),它描述了我存储中的路径以允许选择的图像,(还声明了第二个变量(fileRef)负责允许引用或路径选择的图像存储在 Firebase 的存储中)然后访问 AngularFireStorage 的服务,该服务声明为在构造函数中初始化的变量我提交方法上传(),作为参数传递我想在我的存储中建立的路径以及用户选择并在变量 selectedImg 中允许的图像,然后通过方法 snapshotChanges 返回和 Observable,并使用其他方法 pipe() 我包括 finalize(),它基本上模拟 snapshotChanges() 抛出的可观察对象执行最终function 当此方法的流程结束时,在本例中通过变量 fileRef 访问方法 getDownloadUrl

All aparently works in the form well, but when i try to submit to Save the image in my Firebase this is the error that shows up一切显然都在表格中很好地工作,但是当我尝试提交以将图像保存在我的 Firebase 中时,这是出现的错误

在此处输入图像描述

Im struggling by myself trying to learn Angular, thus would appreciate any idea about what i should do referring to this.Thanks in advance!!!我自己努力学习 Angular,因此我将不胜感激关于我应该做什么的任何想法。提前致谢!

It seems you have not initialized Firebase in app.module.ts and linked your firebaseConfig defined in environment.ts看来您还没有在 app.module.ts 中初始化app.module.ts并链接了您在environment.ts中定义的firebaseConfig

Import AngularFireModule and add it to the imports array initializing it.导入AngularFireModule并将其添加到初始化它的导入数组中。

...
import { AngularFireModule } from '@angular/fire';

@NgModule({
  declarations: [
     ...
  ],
  imports: [
    ...
    AngularFireModule.initializeApp(environment.firebaseConfig),
  ],
  providers: [
     ...
  ],
  ...
})
export class AppModule { }

Hope it helps.希望能帮助到你。 Happy coding!编码愉快!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM