I found similar posts however I didn't understand or its didn't work for me. I have a method which I use for get company detail from database. Similar methods worked until now but this method isn't working now. Where am I doing wrong? When I want to access company object, it returns undefined. I have never faced such a problem until today.
user-component.company.ts
export class UserCompanyComponent implements OnInit {
customerForm: FormGroup;
userId: number;
company: CompanyRequestModel;
constructor(
private toastrService: ToastrService,
private userService: UserService,
private customerService: CustomerService,
private authService: AuthService,
private formBuilder: FormBuilder
) { }
ngOnInit() {
this.authService.userDetailFromToken();
this.userId = this.authService.userId;
this.getCompany();
console.log(this.company);
}
createCustomerForm() {
this.customerForm = this.formBuilder.group({
companyName: ["", Validators.required]
});
}
isAlreadyCompany(): boolean {
if (this.company.userId == this.userId) {
return true;
} else {
return false;
}
}
getCompany() {
this.customerService.getById(this.userId).subscribe(response => {
this.company = {
companyName: response.data.companyName,
userId: response.data.userId
}
})
}
}
customer.service.ts
@Injectable({
providedIn: 'root'
})
export class CustomerService {
constructor(private httpClient : HttpClient) { }
getCustomers() : Observable<ListResponseModel<Customer>>{
return this.httpClient.get<ListResponseModel<Customer>>(GlobalConstants.apiUrl + "customers/getalldetail");
}
getById(id: number) : Observable<SingleResponseModel<CompanyRequestModel>>{
return this.httpClient.get<SingleResponseModel<CompanyRequestModel>>(GlobalConstants.apiUrl + "customers/getbyid?id=" + id);
}
addCustomer(companyRequest: CompanyRequestModel): Observable<ResponseModel>{
return this.httpClient.post<ResponseModel>(GlobalConstants.apiUrl + "customers/add", companyRequest);
}
}
I am new on Angular. That's why I can't find where is the problem. response.data variable returns data from api however when I try to bind with a object, that object returns undefined.