cspan00
cspan00

Reputation: 41

Cannot get firebaseRef to work with angular2 "Cannot find name 'firebase'."

We are trying to get this example to run in our Angular 2 app from the angularFire2 api docs: angularfire2.com/api

import {Inject} from 'angular2/core';
import {FirebaseRef} from 'angularfire2';

class MyComponent {
constructor(@Inject(FirebaseRef) ref:Firebase) {
ref.on('value', this.doSomething);
  }
}

but we keep getting a build error "cannot find name Firebase"

Where do we need to define Firebase? Its already defined in typings.json :

"ambientDependencies": {
"firebase": "github:DefinitelyTyped/DefinitelyTyped/firebase/firebase.d.ts#64b25f63f0ec821040a5d3e049a976865062ed9d",
"es6-shim": "registry:dt/es6-shim#0.31.2+20160317120654"

},

any insight would be appreciated.

Upvotes: 3

Views: 476

Answers (1)

whatsthatitspat
whatsthatitspat

Reputation: 721

This is taken from an answer to this github issue which worked for me when I couldn't get the same code you tried to work.


With Angularfire2, you should not need to write any .on() or .off(). You can accomplish the same thing by pointing to a reference and then subscribing to it, like so:

let subscription = this.af.database.object('someLocation').subscribe(data=> {
//do something with your data
})

Which is essentially the same as:

firebase.database().ref('someLocation').on('value', snapshot=>{
//do something with your data
})

Upvotes: 1

Related Questions