bradtraversy / meanauthapp

Complete MEAN stack app with authentication
242 stars 152 forks source link

Property 'user' does not exist on type 'Object' #54

Open Pradeepreddy580 opened 3 years ago

Pradeepreddy580 commented 3 years ago

I am getting "Property 'user' does not exist on type 'Object' " error. error profile.component.ts code `import { Component, OnInit } from '@angular/core'; import {AuthService} from '../../services/auth.service'; import {Router} from '@angular/router';

@Component({ selector: 'app-profile', templateUrl: './profile.component.html', styleUrls: ['./profile.component.css'] }) export class ProfileComponent implements OnInit { user:Object;

constructor(private AuthService:AuthService, private router: Router) { }

ngOnInit() { this.AuthService.getProfile().subscribe(profile => { this.user = profile.user; }, err => { console.log(err); return false; }); }

} ` auth.service.ts code

`import { Injectable } from '@angular/core'; import { HttpClient, HttpClientModule,HttpHeaders } from '@angular/common/http'; import 'rxjs/add/operator/map'; import { map } from 'rxjs/operators';

@Injectable({ providedIn: 'root' }) export class AuthService { authToken:any; user: any;

constructor(private http:HttpClient) { }

registerUser(user){ let headers = new HttpHeaders(); headers.append('Content-Type','application/json'); return this.http.post('http://localhost:3000/users/register',user, {headers: headers});

} authenticateUser(user){ let headers = new HttpHeaders(); headers.append('Content-Type','application/json'); return this.http.post('http://localhost:3000/users/authenticate',user, {headers: headers});

} getProfile(){ let headers = new HttpHeaders(); this.loadToken(); headers.append('Authorization', this.authToken); headers.append('Content-Type','application/json'); return this.http.get('http://localhost:3000/users/profile', {headers: headers}); } storeUserData(token, user){ localStorage.setItem('id_token', token); localStorage.setItem('user', JSON.stringify(user)); this.authToken=token; this.user=user; } loadToken(){ const token = localStorage.getItem('id_token'); this.authToken = token; } logout(){ this.authToken=null; this.user=null; localStorage.clear(); } } ` Anybody can help me.