Sometimes function node does not work (ERROR: Cannot read properties of undefined (reading '0'))

Hello can you please help me why I am encountering this problem. I use Google custom search api to return results. This is what I am getting

[
{
"kind": "customsearch#search",
"url": {...}, // 2 keys
"queries": {...}, // 2 keys
"context": {...}, // 1 keys
"searchInformation": {
"searchTime": 0.8617,
"formattedSearchTime": "0.86",
"totalResults": "3100000",
"formattedTotalResults": "3,100,000"
},
"items": [
{
"kind": "customsearch#result",
"title": "Google Home Mini Smart Speaker with Google Assistant Charcoal ...",
"htmlTitle": "<b>Google Home</b> Mini Smart Speaker with Google Assistant Charcoal ...",
"link": "https://www.ebay.com/itm/254589757291",
"displayLink": "www.ebay.com",
"snippet": "Chalk GREY or. It's your own personal Google. Use your voice to quickly find information about the weather, news, sports and more. Get help with things like ...",
"htmlSnippet": "Chalk GREY or. It&#39;s your own personal <b>Google</b>. Use your voice to quickly find information about the weather, news, sports and more. Get help with things like&nbsp;...",
"cacheId": "EG90Z3DW7MEJ",
"formattedUrl": "https://www.ebay.com/itm/254589757291",
"htmlFormattedUrl": "https://www.ebay.com/itm/254589757291",
"pagemap": {
"offer": [
{
"availableatorfrom": "Egg Harbor Township, New Jersey, United States",
"price": "24.99",
"itemcondition": "Open box"
}
],
"cse_thumbnail": [
{
"src": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQlY_RZSBWP2iiPmYLnzgNZ-2Gbx8JBp_Y2BBQVH1CkDyNcc_Ik9T_16PWl",
"width": "292",
"height": "172"
}
],
"product": [
{
"image": "https://i.ebayimg.com/thumbs/images/g/aOMAAOSwB7Fhu5jN/s-l96.jpg",
"name": "Details about Google Home Mini Smart Speaker with Google Assistant Charcoal Chalk Coral Aqua",
"model": "Google Home Mini"
}
],
"metatags": [
{
"og:image": "https://i.ebayimg.com/images/g/L1YAAOSw2upesxra/s-l400.jpg",
"og:type": "ebay-objects:item",
"twitter:card": "summary",
"twitter:title": "Google Home Mini Smart Speaker with Google Assistant Charcoal Chalk Coral Aqua",
"og:site_name": "eBay",
"msvalidate.01": "34E98E6F27109BE1A9DCF19658EEEE33",
"og:title": "Google Home Mini Smart Speaker with Google Assistant Charcoal Chalk Coral Aqua | eBay",
"yandex-verification": "6e11485a66d91eff",
"y_key": "acf32e2a69cbc2b0",
"og:description": "Chalk GREY or. It's your own personal Google. Use your voice to quickly find information about the weather, news, sports and more. Get help with things like your schedule, commute, travel information and more.",
"twitter:image": "https://i.ebayimg.com/images/g/L1YAAOSw2upesxra/s-l400.jpg",
"layout": "main",
"referrer": "unsafe-url",
"fb:app_id": "102628213125203",
"twitter:site": "@eBay",
"twitter:description": "Chalk GREY or. It's your own personal Google. Use your voice to quickly find information about the weather, news, sports and more. Get help with things like your schedule, commute, travel information and more.",
"og:url": "https://www.ebay.com/itm/254589757291"
}
],
"cse_image": [
{
"src": "https://i.ebayimg.com/images/g/L1YAAOSw2upesxra/s-l400.jpg"
}
],
"brand": [
{
"name": "Google"
}
],
"hproduct": [
{
"fn": "Details about Google Home Mini Smart Speaker with Google Assistant Charcoal Chalk Coral Aqua",
"photo": "https://i.ebayimg.com/thumbs/images/g/VCwAAOSwlThd-6R~/s-l200.jpg",
"currency": "USD",
"currency_iso4217": "840"
}
],
"listitem": [
{
"item": "Consumer Electronics",
"name": "Consumer Electronics",
"position": "1"
},
{
"item": "Surveillance & Smart Home Electronics",
"name": "Surveillance & Smart Home Electronics",
"position": "2"
},
{
"item": "Smart Speakers, Hubs & Accessories",
"name": "Smart Speakers, Hubs & Accessories",
"position": "3"
},
{
"item": "Smart Speakers",
"name": "Smart Speakers",
"position": "4"
}
]
}
},
{...}, // 11 keys
{...}, // 11 keys
{...}, // 11 keys
{...}, // 11 keys
{...}, // 11 keys
{...}, // 11 keys
{...}, // 11 keys
{...}, // 11 keys
{...} // 11 keys
]
}
]

and I try to get the title, price and link using this code:

var data = [];

counter = 0;

for (item in items[0].json.items){
  data.push({
  json:{
  "title":items[0].json.items[counter].title,
  "url":items[0].json.items[counter].link,
  "price":items[0].json.items[counter].pagemap.offer[0].price
  }
  });
  counter = counter +1;

}

return data;

The problem is at some instances it works and sometime it does not. I also do not see any changes in the structure of response from the API. The error I have been getting is this:

ERROR: Cannot read properties of undefined (reading ‘0’)

Hi @aero, sorry for the late reply - I have completely missed this thread. Would you be able to provide some example data where this is not working (seeing as the example you have provided does work)?

Solely based on the description provided I suspect .json.items[counter].pagemap.offer might not exist for all responses, so you might need to add a little if/else logic.

2 Likes

Sorry for the late reply. It was fixed now. Completely redesigned the code and found out that you were right. There were instances that .json.items[counter].pagemap.offer returns a different value. Thanks

1 Like