Reputation: 339
I have a Company and a Contact class, I have service for both. This is how the classes look like:
The Company class:
export class Company{
id: number;
logo: string;
name: string;
phone: string;
email: string;
website: string;
facebook: string;
country_code: string;
hq_country: string;
hq_zipcode: number;
hq_settlement: string;
hq_address: string;
bi_name: string;
bi_country: string;
bi_zipcode: number;
bi_settlement: string;
bi_address: string;
taxnumber: number;
mail_name: string;
mail_country: string;
mail_zipcode: number;
mail_settlement: string;
mail_address: string;
industry_id: number;
employeesnum_id: number;
yearlyincome_id: number;
founded: number;
selected: boolean;
project: number[];
}
The Contact class:
import { Company } from './company';
export class Contact{
id: number;
company: Company[];
full_name: string;
surname: string;
middle_name: string;
forename: string;
nickname: string;
phone: string;
email: string;
primary_communication_chanel: string;
rank: string;
greeting: string;
selected: boolean;
}
In their services, there are an add method, where you can add new company (or contact) to the in memory web api:
this is very similar in the 2 services:
in company.service:
addCompany(company: Company): Observable<Company>{
return this.http.post<Company>(this.companiesUrl, company, httpOptions).pipe(
catchError(this.handleError<Company>('addHero'))
);
}
in contact.service:
addContact(contact: Contact): Observable<Contact>{
return this.http.post<Contact>(this.contactsUrl, contact, httpOptions).pipe(
catchError(this.handleError<Contact>('addContact'))
);
}
And I have components to companies and contacts, where I display the companies/contacts I have. In this components you can add new company/contact instat. Instat means you only have to give the name of the company/contact.
This is the methods to add instant company/contact:
in companies.compontent:
addInstant(name: string): void{
name = name.trim();
if (!name) { return; }
this.companiesService.addCompany({ name } as Company)
.subscribe(company => {
this.companies.push(company);
});
}
in contats.component:
addInstant(name: string): void{
name = name.trim();
if (!name) { return; }
this.contactsService.addContact({ name } as Contact)
.subscribe(contact => {
this.contacts.push(contact);
});
}
This 2 is really similar. The problem is in the companies.component is work, no error, but in the contacts.component there is an error: Type '{ name: string; }' cannot be converted to type 'Contact'. Property 'id' is missing in type '{ name: string; }'. If I add the id manually, still have an error ("Property 'company' is missing...").
What is the problem?
Upvotes: 0
Views: 859
Reputation: 8183
If you take a look at your Contact
class, there is no property with the name of name
. Really I'd recommend you alter your services to pass in a string and change the web API to accept the string rather than a whole object.
If you are not trying to set the name
property you can do this:
this.contactsService.addContact({ fullName: name } as Contact)
Upvotes: 1