2

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: ""
}

2 Answers 2

4

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;
}
1

Since FirebaseApp is a service you need to inject in the constructor.Only the variables has to be declared above constructor.Make sure you import FirebaseApp.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.