I have populated my object Contract when using the debugger I can see it has populated the object. But later when I want to use the objects fields but it is undefined. I do not understand why this is the case. Here is the line in question where contract is undefined.
console.log("test id contract client" + this.contract.contractId);
And Here is my full code
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Client } from '../../models/client';
import { Contract } from '../../models/Contract';
import { ClientService } from '../../services/client.service';
import { ContractService } from '../../services/contract.service';
@Component({
selector: 'app-contract-customer',
templateUrl: './contract-customer.component.html',
styleUrls: ['./contract-customer.component.css']
})
export class ContractCustomerComponent implements OnInit {
public contractNumber: string;
public client: Client;
public contract: Contract;
constructor(private route: ActivatedRoute, private clientService: ClientService, private contractService: ContractService) { }
ngOnInit():void {
// First get the contract id from the current route.
const url = window.location.href;
var parts = url.split('/', 6);
this.contractNumber = parts[4];
console.log("Contract Id form url : " + this.contractNumber);
this.GetContractClient();
}
GetContract(id: string) {
this.contractService.getContract(Number(id)).subscribe(result => {
this.contract = result;
}, error => {
console.log(error)
});
}
GetContractClient() {
this.GetContract(this.contractNumber);
console.log("test id contract client" + this.contract.contractId);
this.clientService.getClient(this.contract.contractId).subscribe(result => {
this.client = result;
}, error => {
console.log('something wrong here!!!')
console.log(error)
});
}
}