0

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.

ATK
  • 103
  • 1
  • 11
  • 1
    The `console.log(this.company)` after the `this.getCompany();` call would show `undefined` because the variable is assigned asynchronously. The `console.log` must be inside the subscription to work as expected. If you say the `response.data` is undefined, then do a `console.log(response)` inside the subscription and check if the `data` property is defined. – ruth Mar 28 '21 at 17:18
  • 1
    You could learn more about async calls in [this](https://stackoverflow.com/q/14220321/6513921) canonical post. – ruth Mar 28 '21 at 17:29
  • 1
    You can see imagine that when you write `this.getCompany()` only say Angular "search my data", not wait to get my data. – Eliseo Mar 28 '21 at 20:01

0 Answers0