简体   繁体   中英

Upload file to Firebase Storage using AngularFire2

I am working on a project with Angular2 and Firebase and I am willing to upload images to Firebase Storage.

So, I created the Component with the contructor and the function, and, then, the template. I am not sure whether the way I declare the variable firebaseApp, public firebaseApp: FirebaseApp; is correct.

So, in my component, I have:

import { Component, OnInit, Inject, Input } from '@angular/core';
import {AngularFireDatabase, FirebaseListObservable} from 'angularfire2/database';
import { MdDialog, MdDialogRef, MD_DIALOG_DATA, MdButton } from '@angular/material';
import { Http, Headers, Response } from '@angular/http';
import {Observable} from 'rxjs/Rx';
import { FormBuilder, FormGroup } from '@angular/forms';
import { FirebaseApp } from 'angularfire2';
import 'firebase/storage';

@Component({
  selector: 'individual-chat',
  templateUrl: './chat.component.html',
  styleUrls: ['./chat.component.css']
})

export class IndividualChat implements OnInit {
    public firebaseApp: FirebaseApp;
constructor(
    firebaseApp: FirebaseApp,
    public afAuth: AngularFireAuth, 
    public af: AngularFireDatabase,
    public http: Http,
    public dialog: MdDialog
  ){ }

   EditChatData(){
      this.af.object('groupConversations/'+ this.my_id + '/' +this.conversation_id ).update({ groupName: this.group_edit_data.name ? this.group_edit_data.name : 'New Group' });        

  }
    onChange(files) {
    console.log(files, files[0]);
      let storageRef = this.firebaseApp.storage().ref().child('groupImages');

      storageRef.put(files[0]).then( snapshot => {
        console.log('successfully added');        
      }).catch(err => { console.error("Whoupsss!", err) })
    }
  ...
}

And the form I created in my template looks like this:

<form name="form" (ngSubmit)="EditChatData()" enctype="multipart/form-data">
  <input type="file" #file (change)="onChange(file.files)" />
  <input type="text" placeholder="Group name" name="name" [(ngModel)]="group_edit_data.name" #name="ngModel" class="form-control" value=" {{ group_name }}" />
  <button class="btn">DONE</button>
</form>

I keep getting this error:

ERROR TypeError: Cannot read property 'storage' of undefined

And, the console.log in my onChange(files) return this object:

FileList {0: File, length: 1}
length: 1,
0: File {
lastModified: 1503324156295
lastModifiedDate: Mon Aug 21 2017 17:02:36 GMT+0300 (EEST),
name:"Screenshot from 2017-08-21 17-02-36.png",
size: 401442,
type: "image/png",
webkitRelativePath: ""
}

Cannot read property 'storage' of undefined tells you that this.firebaseApp is undefined .

You can initialize it in the constructor, this way:

constructor(
  firebaseApp: FirebaseApp,
  public afAuth: AngularFireAuth, 
  public af: AngularFireDatabase,
  public http: Http,
  public dialog: MdDialog
) {
  this.firebaseApp = firebaseApp;
}

由于FirebaseApp是一项服务,因此您需要注入构造函数,只需在构造函数上方声明变量即可。确保导入FirebaseApp。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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