ERROR TypeError: Cannot read property 'products' of undefined in Angular 6, problem with model












0















I have problem with model.
I have two models category and product.
Category model is :



export class CategoryModel {

public id: number;
public name: string;
public description: string;
public image: string;
public products: ProductModel ;


constructor(name: string, desciption: string, image: string = null, products: ProductModel, id: number) {
this.id = id;
this.name = name;
this.description = desciption;
this.image = image;
this.products = products;
}

}


product model is :



export class ProductModel {
constructor(

public id:number, public name: string, public description: string, public numberOfProduct: number, public image: string) {
}
}


I store date in the productArray: ProductModel = ; from the server with this method:



 getCategory() {
this.dataStorageServiceService.getCategory().subscribe(category => {
console.log(category);
this.categoryList = category;
console.log(this.categoryList);

})
}


And I want to store many products in the categoryList with this method:



 storeProduct() {
let index = 0;

for (let category of this.categoryList) {
index++;
if (category.name == this.productForm.value.category) {
this.product.name = this.productForm.value.name;
this.product.image = this.productForm.value.image;
this.product.description = this.productForm.value.description;

this.categoryList[index].products.push(this.product);

}

}

this.dataStorageServiceService.storeProduct(this.categoryList[index], index);

}


The product is product = <ProductModel>{};
And I have error ERROR TypeError: Cannot read property 'products' of undefined










share|improve this question























  • Yeah well of course, you update your index before accessing your array item. So if your array is 10 items, you will try to access the 11th one. See the issue ? Move index++ to the end of the loop.

    – trichetriche
    Nov 13 '18 at 13:53











  • (Also, if you need an index in your loop, why don't you just use a for loop instead of a for ...of one ?)

    – trichetriche
    Nov 13 '18 at 13:54











  • But I have error when I want to access zero element and in product I want to add new product with this code : this.categoryList[0].products.push(new ProductModel(1, '2', '2', 2, 'hshs'))

    – Rujan hu
    Nov 13 '18 at 13:58











  • i gave you your error : if it persists, please provide a Minimal, Complete, and Verifiable example reproducing the issue.

    – trichetriche
    Nov 13 '18 at 13:59











  • before add anything.. what is the this.categoryList

    – lesiano
    Nov 13 '18 at 14:22
















0















I have problem with model.
I have two models category and product.
Category model is :



export class CategoryModel {

public id: number;
public name: string;
public description: string;
public image: string;
public products: ProductModel ;


constructor(name: string, desciption: string, image: string = null, products: ProductModel, id: number) {
this.id = id;
this.name = name;
this.description = desciption;
this.image = image;
this.products = products;
}

}


product model is :



export class ProductModel {
constructor(

public id:number, public name: string, public description: string, public numberOfProduct: number, public image: string) {
}
}


I store date in the productArray: ProductModel = ; from the server with this method:



 getCategory() {
this.dataStorageServiceService.getCategory().subscribe(category => {
console.log(category);
this.categoryList = category;
console.log(this.categoryList);

})
}


And I want to store many products in the categoryList with this method:



 storeProduct() {
let index = 0;

for (let category of this.categoryList) {
index++;
if (category.name == this.productForm.value.category) {
this.product.name = this.productForm.value.name;
this.product.image = this.productForm.value.image;
this.product.description = this.productForm.value.description;

this.categoryList[index].products.push(this.product);

}

}

this.dataStorageServiceService.storeProduct(this.categoryList[index], index);

}


The product is product = <ProductModel>{};
And I have error ERROR TypeError: Cannot read property 'products' of undefined










share|improve this question























  • Yeah well of course, you update your index before accessing your array item. So if your array is 10 items, you will try to access the 11th one. See the issue ? Move index++ to the end of the loop.

    – trichetriche
    Nov 13 '18 at 13:53











  • (Also, if you need an index in your loop, why don't you just use a for loop instead of a for ...of one ?)

    – trichetriche
    Nov 13 '18 at 13:54











  • But I have error when I want to access zero element and in product I want to add new product with this code : this.categoryList[0].products.push(new ProductModel(1, '2', '2', 2, 'hshs'))

    – Rujan hu
    Nov 13 '18 at 13:58











  • i gave you your error : if it persists, please provide a Minimal, Complete, and Verifiable example reproducing the issue.

    – trichetriche
    Nov 13 '18 at 13:59











  • before add anything.. what is the this.categoryList

    – lesiano
    Nov 13 '18 at 14:22














0












0








0








I have problem with model.
I have two models category and product.
Category model is :



export class CategoryModel {

public id: number;
public name: string;
public description: string;
public image: string;
public products: ProductModel ;


constructor(name: string, desciption: string, image: string = null, products: ProductModel, id: number) {
this.id = id;
this.name = name;
this.description = desciption;
this.image = image;
this.products = products;
}

}


product model is :



export class ProductModel {
constructor(

public id:number, public name: string, public description: string, public numberOfProduct: number, public image: string) {
}
}


I store date in the productArray: ProductModel = ; from the server with this method:



 getCategory() {
this.dataStorageServiceService.getCategory().subscribe(category => {
console.log(category);
this.categoryList = category;
console.log(this.categoryList);

})
}


And I want to store many products in the categoryList with this method:



 storeProduct() {
let index = 0;

for (let category of this.categoryList) {
index++;
if (category.name == this.productForm.value.category) {
this.product.name = this.productForm.value.name;
this.product.image = this.productForm.value.image;
this.product.description = this.productForm.value.description;

this.categoryList[index].products.push(this.product);

}

}

this.dataStorageServiceService.storeProduct(this.categoryList[index], index);

}


The product is product = <ProductModel>{};
And I have error ERROR TypeError: Cannot read property 'products' of undefined










share|improve this question














I have problem with model.
I have two models category and product.
Category model is :



export class CategoryModel {

public id: number;
public name: string;
public description: string;
public image: string;
public products: ProductModel ;


constructor(name: string, desciption: string, image: string = null, products: ProductModel, id: number) {
this.id = id;
this.name = name;
this.description = desciption;
this.image = image;
this.products = products;
}

}


product model is :



export class ProductModel {
constructor(

public id:number, public name: string, public description: string, public numberOfProduct: number, public image: string) {
}
}


I store date in the productArray: ProductModel = ; from the server with this method:



 getCategory() {
this.dataStorageServiceService.getCategory().subscribe(category => {
console.log(category);
this.categoryList = category;
console.log(this.categoryList);

})
}


And I want to store many products in the categoryList with this method:



 storeProduct() {
let index = 0;

for (let category of this.categoryList) {
index++;
if (category.name == this.productForm.value.category) {
this.product.name = this.productForm.value.name;
this.product.image = this.productForm.value.image;
this.product.description = this.productForm.value.description;

this.categoryList[index].products.push(this.product);

}

}

this.dataStorageServiceService.storeProduct(this.categoryList[index], index);

}


The product is product = <ProductModel>{};
And I have error ERROR TypeError: Cannot read property 'products' of undefined







angular typescript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 13:46









Rujan huRujan hu

13




13













  • Yeah well of course, you update your index before accessing your array item. So if your array is 10 items, you will try to access the 11th one. See the issue ? Move index++ to the end of the loop.

    – trichetriche
    Nov 13 '18 at 13:53











  • (Also, if you need an index in your loop, why don't you just use a for loop instead of a for ...of one ?)

    – trichetriche
    Nov 13 '18 at 13:54











  • But I have error when I want to access zero element and in product I want to add new product with this code : this.categoryList[0].products.push(new ProductModel(1, '2', '2', 2, 'hshs'))

    – Rujan hu
    Nov 13 '18 at 13:58











  • i gave you your error : if it persists, please provide a Minimal, Complete, and Verifiable example reproducing the issue.

    – trichetriche
    Nov 13 '18 at 13:59











  • before add anything.. what is the this.categoryList

    – lesiano
    Nov 13 '18 at 14:22



















  • Yeah well of course, you update your index before accessing your array item. So if your array is 10 items, you will try to access the 11th one. See the issue ? Move index++ to the end of the loop.

    – trichetriche
    Nov 13 '18 at 13:53











  • (Also, if you need an index in your loop, why don't you just use a for loop instead of a for ...of one ?)

    – trichetriche
    Nov 13 '18 at 13:54











  • But I have error when I want to access zero element and in product I want to add new product with this code : this.categoryList[0].products.push(new ProductModel(1, '2', '2', 2, 'hshs'))

    – Rujan hu
    Nov 13 '18 at 13:58











  • i gave you your error : if it persists, please provide a Minimal, Complete, and Verifiable example reproducing the issue.

    – trichetriche
    Nov 13 '18 at 13:59











  • before add anything.. what is the this.categoryList

    – lesiano
    Nov 13 '18 at 14:22

















Yeah well of course, you update your index before accessing your array item. So if your array is 10 items, you will try to access the 11th one. See the issue ? Move index++ to the end of the loop.

– trichetriche
Nov 13 '18 at 13:53





Yeah well of course, you update your index before accessing your array item. So if your array is 10 items, you will try to access the 11th one. See the issue ? Move index++ to the end of the loop.

– trichetriche
Nov 13 '18 at 13:53













(Also, if you need an index in your loop, why don't you just use a for loop instead of a for ...of one ?)

– trichetriche
Nov 13 '18 at 13:54





(Also, if you need an index in your loop, why don't you just use a for loop instead of a for ...of one ?)

– trichetriche
Nov 13 '18 at 13:54













But I have error when I want to access zero element and in product I want to add new product with this code : this.categoryList[0].products.push(new ProductModel(1, '2', '2', 2, 'hshs'))

– Rujan hu
Nov 13 '18 at 13:58





But I have error when I want to access zero element and in product I want to add new product with this code : this.categoryList[0].products.push(new ProductModel(1, '2', '2', 2, 'hshs'))

– Rujan hu
Nov 13 '18 at 13:58













i gave you your error : if it persists, please provide a Minimal, Complete, and Verifiable example reproducing the issue.

– trichetriche
Nov 13 '18 at 13:59





i gave you your error : if it persists, please provide a Minimal, Complete, and Verifiable example reproducing the issue.

– trichetriche
Nov 13 '18 at 13:59













before add anything.. what is the this.categoryList

– lesiano
Nov 13 '18 at 14:22





before add anything.. what is the this.categoryList

– lesiano
Nov 13 '18 at 14:22












1 Answer
1






active

oldest

votes


















0














I don't know what you're trying to do with index so I will not try to edit your whole function, but try initializing the objects this.product and this.categoryList[index].products before interacting with them:



this.categoryList[index].products = new Array<ProductModel>();

this.product = new ProductModel();


Hope this helps at all.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282436%2ferror-typeerror-cannot-read-property-products-of-undefined-in-angular-6-prob%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I don't know what you're trying to do with index so I will not try to edit your whole function, but try initializing the objects this.product and this.categoryList[index].products before interacting with them:



    this.categoryList[index].products = new Array<ProductModel>();

    this.product = new ProductModel();


    Hope this helps at all.






    share|improve this answer






























      0














      I don't know what you're trying to do with index so I will not try to edit your whole function, but try initializing the objects this.product and this.categoryList[index].products before interacting with them:



      this.categoryList[index].products = new Array<ProductModel>();

      this.product = new ProductModel();


      Hope this helps at all.






      share|improve this answer




























        0












        0








        0







        I don't know what you're trying to do with index so I will not try to edit your whole function, but try initializing the objects this.product and this.categoryList[index].products before interacting with them:



        this.categoryList[index].products = new Array<ProductModel>();

        this.product = new ProductModel();


        Hope this helps at all.






        share|improve this answer















        I don't know what you're trying to do with index so I will not try to edit your whole function, but try initializing the objects this.product and this.categoryList[index].products before interacting with them:



        this.categoryList[index].products = new Array<ProductModel>();

        this.product = new ProductModel();


        Hope this helps at all.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 15:18

























        answered Nov 13 '18 at 15:12









        MiloMilo

        1,89361529




        1,89361529






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282436%2ferror-typeerror-cannot-read-property-products-of-undefined-in-angular-6-prob%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine