angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Firebase Storage is not working with ionic 3 & AngularFire 4.0.0 rc

Open raviranjan290620 opened this issue 9 years ago • 16 comments

**Angular:**Angular 4

**Firebase:**3.9.0

**AngularFire:**4.0.0rc

import * as firebase from 'firebase';

const storageRef = firebase.storage().ref().child('images/image.png'); storageRef.getDownloadURL().then(url => this.image = url);

error--> firebase.storage() takes either no argument or a Firebase App instance

raviranjan290620 avatar Jun 02 '17 08:06 raviranjan290620

In your parent module, you should have import 'firebase/storage and in the component where you're using storage, you should have import { FirebaseApp } from 'angularfire2';

Please search the issues before posting an error like this. It's been asked and answered a few times already.

markgoho avatar Jun 02 '17 12:06 markgoho

Sorry @markgoho but how to use the import 'firebase/storage'

**'import * as firebaseStorge from 'firebase/storage';

@Component({ selector: 'page-home', templateUrl: 'home.html' }) export class HomePage {

storageAvatarRef: any; constructor(public navCtrl: NavController, public platform: Platform, private pdfmake: PdfmakeService, ) { this.storageAvatarRef = firebaseStorge.ref.getDownloadURL; }

When i run the server i get this

[Error]

core.es5.js:1084 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'getDownloadURL' of undefined TypeError: Cannot read property 'getDownloadURL' of undefined at new HomePage (home.ts:25) at createClass (core.es5.js:10883) at createDirectiveInstance (core.es5.js:10701) at createViewNodes (core.es5.js:12064) at createRootView (core.es5.js:11969) at callWithDebugContext (core.es5.js:13184) at Object.debugCreateRootView [

AndradeSoto avatar Jun 04 '17 04:06 AndradeSoto

@markgoho and @jamesdaniels Sorry But its not working with ionic 3 //error TypeError: this.fb.storage is not a function

//Module import {NgModule} from '@angular/core'; import {IonicPageModule} from 'ionic-angular'; import {ProfilePage} from './profile'; import 'firebase/storage' @NgModule({ declarations: [ ProfilePage, ], imports: [ IonicPageModule.forChild(ProfilePage), ], exports: [ ProfilePage ] }) export class ProfilePageModule { } //components

import {Component, Inject} from '@angular/core'; import {NavController, IonicPage} from 'ionic-angular'; import {NgForm} from "@angular/forms"; import {AngularFireAuth} from 'angularfire2/auth'; import {AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable} from 'angularfire2/database'; import * as firebase from 'firebase'; // for typings import { FirebaseApp } from 'angularfire2';

@IonicPage() @Component({ selector: 'page-profile', templateUrl: 'profile.html', }) export class ProfilePage {

file:any; forUpLoadRef: FirebaseListObservable; userRef: FirebaseObjectObservable; storageRef:any;

constructor(public navCtrl: NavController, public af: AngularFireAuth, public db: AngularFireDatabase, private fb: FirebaseApp) {}

UploadNewImage(){ this.file = (document.getElementById('inputFileId')).files[0]; console.log("file recived"+ JSON.stringify(this.file.name)); let storageRef = this.fb.storage().ref(); var metadata = { contentType: 'image/*' };

this.storageRef.child('images/'+this.file.name).put(this.file,metadata)

}

}

//error TypeError: this.fb.storage is not a function

raviranjan290620 avatar Jun 07 '17 12:06 raviranjan290620

try this: import * as firebase from 'firebase/app'; import 'firebase/storage';

chunxchun avatar Jun 07 '17 18:06 chunxchun

Sorry To disturb you @chunxchun but its not working yet after putting your code

Uncaught (in promise): storage: Firebase: firebase.storage() takes either no argument or a Firebase App instance. (app/invalid-app-argument).

raviranjan290620 avatar Jun 08 '17 05:06 raviranjan290620

in my case, I put both lines in a provider (not in Module) and then inject it to components that need access to the firebase storage, and no need to import { FirebaseApp } from 'angularfire2' anymore.

So in util-provider.ts:

import { AngularFireDatabase, FirebaseObjectObservable, FirebaseListObservable } from 
'angularfire2/database';
import * as firebase from 'firebase/app';
import 'firebase/storage';
//import { FirebaseApp } from 'angularfire2';

and then in the component:

import { UtilProvider } from '../../providers/util-provider';
constructor(public util: UtilProvider) {}

chunxchun avatar Jun 08 '17 06:06 chunxchun

and I think the ref in your code should be let storageRef = firebase.storage().ref(); instead of let storageRef = this.fb.storage().ref();

chunxchun avatar Jun 08 '17 06:06 chunxchun

@markgoho I agree there are many response to this kind of issue. But after 120 google search and looking to issues finally found this one, which you referred to as "Please search the issues before posting an error like this. It's been asked and answered a few times already."

but you are the only one who gave a simple and most clear answer. works. thanks. But no question is a dumb question. the way information are presented may not be understood by everyone.

pk359 avatar Jun 25 '17 11:06 pk359

Hey guys I was having this same problem, but getting an issue where it was saying that my Firebase app had not been initialized. After an hour of debugging, the solution was ending up being including auth and db in the constructor like so:

import { Component } from '@angular/core'
import { AngularFireAuth } from 'angularfire2/auth'
import { AngularFireDatabase } from 'angularfire2/database'
import * as firebase from 'firebase/app'
import 'firebase/storage'

@Component({
  selector: 'page-photos',
  templateUrl: 'photos.html'
})
export class PhotosPage {
  constructor(
    public afAuth: AngularFireAuth,
    public afDb: AngularFireDatabase,
  ) {}

  upload() {
    const storageRef = firebase.storage().ref()
    // ...
  }
}

So even though I don't use auth and db, I still had to include them in the constructor.

sean-hill avatar Aug 21 '17 15:08 sean-hill

+1 sean-hill this worked for me thx

sascha1337 avatar Sep 16 '17 17:09 sascha1337

Thanks @sean-hill You saved my day

aliwaqassi avatar Oct 30 '17 20:10 aliwaqassi

Thanks: @chunxchun it works perfectly.

fcampinho avatar Dec 14 '17 15:12 fcampinho

Thank @sean-hill, my case, I just need Auth and it's worked

thanhtung90 avatar Jan 06 '18 01:01 thanhtung90

good

maximdevFR avatar Apr 11 '18 01:04 maximdevFR

Mine worked by removing AngularFireAuth Thanks @sean-hill

CODEX-Prog avatar Dec 13 '18 20:12 CODEX-Prog

try this: import * as firebase from 'firebase/app'; import 'firebase/storage';

it works for me..thanks

ghost avatar Jan 31 '19 07:01 ghost