Before starting my question, I would like to inform you that I have already done a lot of research and I can not find a solution (explanation) why I get this error.
Also note that I'm brand new to Angular, and I just started to learn how it works.
So, the problem I have is what I put in the title of this question.
I am trying to create a login system using Firebase based on the course that I am buying on Udemy.
The code I use is as follows:
auth.service.ts
import {Injectable} from '@angular/core';
import * as firebase from 'firebase';
@Injectable ()
export class AuthService {
token: string;
singInUser ( email: string, password: string ) {
}
getToken () {
return firebase
.auth ()
.currentUser
.getIdToken ();
}
}
Data-storage.service.ts
// ... Dependencies here
@Injectable ()
export class DataStorageService {
private recipeEndPoint: string = 'https://my-unique-id.firebaseio.com/recipes.json';
private recipeSubscription: Observable<any> = new Observable();
constructor ( private http: Http,
private recipes: RecipeService,
private authService: AuthService ) {}
// other functionality ...
getRecipes () {
const token = this.authService.getToken ();
token.then (
( token: string ) => {
this.recipeSubscription = this.http.get ( this.recipeEndPoint + '?auth=' + token ).map (
( data: Response ) => {
return data.json ();
}
);
// THIS PARTICULAR CODE WORKS AS EXPECTED
// WITH NO ISSUES
this.recipeSubscription.subscribe (
( data: Response ) => {
console.log ( 'Data response: ', data );
},
( error ) => {
console.log ( 'Error: ' + error );
}
)
}
);
// This is supposed to return an Observable to the caller
return this.recipeSubscription;
}
}
header.component.ts
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
constructor(private dataStorage: DataStorageService, private recipeService: RecipeService) { }
onFetchData() {
let recipeSubscription = this.dataStorage.getRecipes();
console.log(recipeSubscription instanceof Observable);
recipeSubscription.subscribe();
setTimeout(
() => {
console.log(recipeSubscription instanceof Observable);
},
500
);
setTimeout(
() => {
console.log(recipeSubscription instanceof Observable);
},
1000
);
setTimeout(
() => {
console.log(recipeSubscription instanceof Observable);
},
1500
);
}
}
So, unfortunately, I do not see what could be wrong with this code. Can someone notice something that I did wrong?
. , . , , , .
# 1
header.component.html
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">Logo Here</div>
<div class="navbar-default">
<ul class="nav navbar-nav">
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown" appDropdown>
<a routerLink="/" class="dropdown-toggle" role="button">Manage <span class="caret"></span></a>
<ul class="dropdown-menu">
<li>
<a style="cursor: pointer;" (click)="onSaveData()">Save Data</a>
</li>
<li>
<a style="cursor: pointer;" (click)="onFetchData()">Fetch Data</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
</nav>