I encountered a type error while attempting to set a property in my user.service.ts. Here is the code snippet from user.service:
import { Injectable } from '@angular/core';
import { UserModel } from '../models/user.model';
@Injectable()
export class UserService {
private _user: UserModel;
constructor() {}
get getUser(): UserModel {
return this._user;
}
set setUser(user: UserModel) {
this._user = user;
}
}
This is the structure of UserModel:
export class UserModel {
constructor(public uid: string,
public displayName: string,
public email: string,
public photoUrl: string,
public providerId: string) {}
}
The issue arises in the auth.service file where I am encountering the error.
import {Injectable} from '@angular/core';
import * as firebase from 'firebase';
import { AngularFireAuth } from 'angularfire2/auth';
import {Router} from '@angular/router';
import { UserModel } from '../models/user.model';
import { UserService } from './user.service';
@Injectable()
export class AuthService {
private _token: string = null;
private _firstLogin = false;
constructor(private afAuth: AngularFireAuth,
private router: Router,
private userService: UserService) {}
get isFirstLogin() {
return this._firstLogin;
}
get getUserToken(): string{
return this._token;
}
set setUserToken(tk: string) {
this._token = tk;
}
// Sign in with Facebook method
signinWithFacebook(): Promise<any> {
const fbProvider = new firebase.auth.FacebookAuthProvider();
return this.signin(this.afAuth.auth.signInWithPopup(fbProvider));
}
signin(popupResult: Promise<any>): Promise<any> {
return popupResult
.then(
(res) => {
this._firstLogin = true;
const user: firebase.User = res.user.toJSON();
const credential = res.credential;
this._token = credential.accessToken;
// Creating an instance of UserModel and passing it to userService's property (_user)
const providedData = user.providerData[0];
const buildedUser = new UserModel(providedData.uid, providedData.displayName,
providedData.email, providedData.photoURL, providedData.providerId);
this.userService.setUser(buildedUser); //THE ERROR OCCURS HERE.
console.log(this._token);
console.log(user);
}
);
}
}
The error occurs when trying to pass a UserModel object from auth.service to user.service using the line: this.userService.setUser(buildedUser). Please provide me with a solution and explain why this issue is happening. Thanks!