oleolematole
oleolematole

Reputation: 13

Angular 2+ and missing property

I'm getting error in my windows console but program runs correctly in chrome and do waht I want. The error:

ERROR in src/app/modules/profile/components/add-comment/add-comment.component.ts(36,37): error TS2345: Argument of type 'Profile' is not assignable to parameter of type 'Profile'. Property 'fullName' is missing in type 'Profile'.

It's sounds weird. I want only to set my Subject profile. What went wrong?

My service:

import { Injectable } from '@angular/core';
import { Profile } from '../models/profile';
import { HttpClient } from '@angular/common/http';
import { Subject, Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ProfileService {

  private _profileUrl: string = "/assets/mocks/profile.json";
  profile$: Subject<Profile> = new Subject<Profile>();

  constructor(private _http: HttpClient) {
    this._http.get(this._profileUrl).subscribe( (resProfile: Profile) => {
      this.profile$.next(resProfile);
    })
  }

  getProfile(): Observable<Profile> {
    return this.profile$.asObservable();
  }

  setProfile(profile: Profile): void {
    this.profile$.next(profile)
  }

}

My component

import { Component, OnInit } from '@angular/core';
import { Profile } from 'selenium-webdriver/firefox';
import { ProfileService } from 'src/app/services/profile.service';

import { Comment } from '../../../../models/comment';

@Component({
  selector: 'app-add-comment',
  templateUrl: './add-comment.component.html',
  styleUrls: ['./add-comment.component.scss']
})
export class AddCommentComponent implements OnInit {

  profile: Profile;
  displayComments: boolean = true;

  constructor(private _profileService: ProfileService) { }

  ngOnInit() {
    this._profileService.getProfile().subscribe( (resProfile: any) => {
      this.profile = resProfile;
    })
  }

  onAddCommentEnter(commentText: string): void {
    let profileTmp: Profile = this.profile;

    let comment: Comment = {
      fullName: 'Tomasz Mitura',
      time: 'now',
      text: commentText
    }

    profileTmp.comments.push(comment);
    console.log('profileTmp', profileTmp);
    this._profileService.setProfile(profileTmp);
  }

}

Upvotes: 1

Views: 172

Answers (2)

Maarti
Maarti

Reputation: 3719

Edit : You didn't import the correct Profile class. Replace

import { Profile } from 'selenium-webdriver/firefox';

by

import { Profile } from '../models/profile';

in your Component (edit the path to your Profile class from your Component).


If you imported the correct Profile class, just restart your console/IDE/ng serve.

It's probably because it kepts an old prototype of Profile in cache.

Upvotes: 1

Woot
Woot

Reputation: 1809

In your component in line 3 notice that you are importing your Profile class from

import { Profile } from 'selenium-webdriver/firefox';

but I believe the one you want is

import { Profile } from '../models/profile';

The same one your service is using.

Upvotes: 0

Related Questions