0

I have this 4-tier if loop which loops through a JSON object from a file. The aim here is to create an array which will be added to a row of a MySQL database.

The loop should filter through the json and then add certain values to an array:

var content = fs.readFileSync("dataSubcats.json");
var jsonContent = JSON.parse(content);

var values = [];

for(let i=0; i<jsonContent.length; i++) {

    if (jsonContent[i].subcategories) {
        let subcat = jsonContent[i].subcategories.length;
        for (let n=0; n<subcat; n++) {

            if(jsonContent[i].subcategories[n]) {
                let subsubcat = jsonContent[i].subcategories[n].facetValueData.length
                for (let p=0; p<subsubcat; p++) {

                    if(jsonContent[i].subcategories[n].facetValueData) {
                        let products = jsonContent[i].subcategories[n].facetValueData[p].productData.length
                        for (let f=0; f<products; f++) {
                            
                            var productDataFinal = jsonContent[i].subcategories[n].facetValueData[p].productData[f]
                            values.push([   
                                jsonContent[i].id, jsonContent[i].categoryName, 
                                jsonContent[i].subcategories[n].name, jsonContent[i].subcategories[n].code, 
                                jsonContent[i].subcategories[n].facetValueData[p].name, jsonContent[i].subcategories[n].facetValueData[p].code,
                                productDataFinal.productDisplayName, productDataFinal.code, 
                                productDataFinal.url, productDataFinal.imageUrl,
                                productDataFinal.price.value, productDataFinal.originPrice.value, 
                                productDataFinal.price.currencyIso, productDataFinal.inStock,
                                productDataFinal.newProduct
                            ])
                        }
                    }
                }
            } else {
                 console.log(`No facetvalue data found at ${jsonContent[i]}`)
        }
        } 
    } else {
        console.log(`No subcats found at ${jsonContent[i]}`)
    }
}

The JSON structure is as follows:

 [
    {
        "id": 0,
        "categoryName": "Beauty",
        "subcategories": [
            {
                "count": 2688,
                "code": "/beauty/skin-care",
                "name": "Skincare",
                "facetValueData": [
                    {
                        "count": 682,
                        "code": "/beauty/skin-care/lotion-serum-and-essence",
                        "name": "Lotion Serum & Essence",
                        "productData": []
                    },
                    {
                        "count": 497,
                        "code": "/beauty/skin-care/moisturiser-and-mist",
                        "name": "Moisturiser & Mist",
                        "productData": []
                    },
                    {
                        "count": 354,
                        "code": "/beauty/skin-care/cleanser-and-exfoliation",
                        "name": "Cleanser & Exfoliation",
                        "productData": []
                    },
                    {
                        "count": 345,
                        "code": "/beauty/skin-care/mask-and-treatment",
                        "name": "Mask & Treatment",
                        "productData": []
                    }
                ]
            },
            {
                "count": 1549,
                "code": "/beauty/makeup",
                "name": "Makeup",
                "facetValueData": [
                    {
                        "count": 653,
                        "code": "/beauty/makeup/face",
                        "name": "Face",
                        "productData": []
                    },
                    {
                        "count": 460,
                        "code": "/beauty/makeup/makeup-lips",
                        "name": "Lips",
                        "productData": []
                    },
                    {
                        "count": 337,
                        "code": "/beauty/makeup/eyes",
                        "name": "Eyes",
                        "productData": []
                    },
                    {
                        "count": 124,
                        "code": "/beauty/makeup/makeup-cheeks",
                        "name": "Cheeks",
                        "productData": []
                    }
                ]
            }
        ]
    },
    {
        "id": 1,
        "categoryName": "Electronics",
        "subcategories": [
            {
                "count": 526,
                "code": "/electronics/audio-devices",
                "name": "Audio",
                "facetValueData": [
                    {
                        "count": 153,
                        "code": "/electronics/audio-devices/speakers",
                        "name": "Speakers",
                        "productData": []
                    },
                    {
                        "count": 145,
                        "code": "/electronics/audio-devices/earphones",
                        "name": "Earphones",
                        "productData": []
                    },
                    {
                        "count": 142,
                        "code": "/electronics/audio-devices/headphones",
                        "name": "Headphones",
                        "productData": []
                    },
                    {
                        "count": 51,
                        "code": "/electronics/audio-devices/true-wireless-and-airpods",
                        "name": "True Wireless & Airpods",
                        "productData": []
                    }
                ]
            }
                    
        ]
    }
]

it continues like this for a total of 10 categoryName objects (I may have gotten some closing brackets wrong here but it is correct in my script).

The loop always breaks with the following error:

let subsubcat = jsonContent[i].subcategories[n].facetValueData.length
                                                                               ^

TypeError: Cannot read property 'length' of undefined
    at Object.<anonymous> (/Users/Desktop/NodeScraper/db.js:18:80)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

I am stumped

5
  • 1
    Could you share the code that tries to get the content from the file? Commented Aug 18, 2020 at 16:16
  • Strange JSON structure. If I print it I got an error... Commented Aug 18, 2020 at 16:20
  • 1
    When I check the JSON, the subcategories array is not closed. The same goes for facetValueData. Can that be the problem? Or is that just an copy-paste error? Commented Aug 18, 2020 at 16:21
  • The JSON is actually much longer than this, this is just for an example Commented Aug 18, 2020 at 16:22
  • 1
    Yes my apologies its just copy and paste error i will fix Commented Aug 18, 2020 at 16:22

1 Answer 1

1

Ciao, you error could be here:

for (let n=0; n<subcat; i++) {...}

try to replace i with n like:

for (let n=0; n<subcat; n++) {...}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, I have changed my code as per your suggestion, unfortunately the error still persists
The error now says let subsubcat = jsonContent[i].subcategories[n].facetValueData.length ^ TypeError: Cannot read property 'length' of undefined
I have figured it out, the if Statement "if(jsonContent[i].subcategories[n].facetValueData)" should have been above the for loop just before it, not below. Thanks for help
No problem. If you think answer was useful please, mark it as valid :).

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.