2

I have an object of array ( form an API ), eatch object contains an array and i can't get access to the sub array:

Structure:

Billing [date, totalPrice, {productList[product1,product2]}, ..., ..., {[...,...,...]},....,....,..., {[...,...,...,]}]

My Model is:

export  class Billing {
  private _productsList: Array<InvoiceLine>;
  private _Date: Date;
  private _totalPrice: number;
}

Then I initialized an my array:

public AllBilling: Billing[] = []; 

Then when I try to get my data from Blling Array it works.

for ( i = 0; i < this.AllBilling.length; i++){
     console.log(this.AllBilling);
}

But when I try to get productsList form my array it doesn't work.

for ( i = 0; i < this.Belling.length; i++){
     console.log(this.Belling[i]);

        for ( j = 0; j < this.AllBilling[i].productsList.length; i++){
          console.log(this.AllBilling[i].productsList[j]); }
}

It gives me the folowing error:

ERROR TypeError: Cannot read property 'length' of undefined

witch refer to productsList.length

enter image description here

1 Answer 1

3

You have several mistakes in this code

  1. what you need to repeat is AllBilling, not Belling, Belling refers to a class, not a value

  2. _productsList not productsList

  3. If you want to access productsList, it should be public, not private

That's why it was not working. Hope this help

Sign up to request clarification or add additional context in comments.

4 Comments

Point 3 is not a should, it is just a suggestion, since javascript will still be able to access the field directly
It's correct. However, if you mark it as private, it'll throw an error when you build it, or even in the IDE. Since you use TypeScript, you should follow it, otherwise it doesn't make sense
and "should" is a suggestion, isn't it :D, not "must"
It will throw an error if you have the option –noEmitOnError

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.