I am trying to push an object using firebase, where angular will act as frontend.
component.html
<h3>Adding an object</h3>
<input type="text" (keyup.enter)="addCourse(course)" #course>
component.ts
import { Component, OnDestroy } from '@angular/core';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { Observable, of, Subscription } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'FirebaseDemo';
// courses: any[];
courses$;
course$;
author$;
todos$: AngularFireList<any>;
// subscription : Subscription;
constructor(db: AngularFireDatabase) {
// code to read the courses.
db.list('/course')
.valueChanges()
.subscribe((inp) => {
this.courses$ = inp;
console.log(this.courses$);
});
}
addCourse(value: string) {
db.list('/course/').set('4', value);
}
}
Here the last instance of db in addCourse(value) function is throwing error called cannot find name db.
How can I access db variable out of constructor or How can I push the object in firebase?
Out of constructor, you can access db by using this.db. Please try the below change in you addCourse function and verify..
addCourse(value: string) {
this.db.list('/course/').set('4', value);}
Also make changes to constructor to make db public/private like:
constructor(public db: AngularFireDatabase)