0

I am making a telegram bot where i am receving data from api call. the api is returning me data like this enter image description here

articles is the array and i want to retrive all the news from this .

when i do it without loop it work fine with no errorenter image description here

But when i am trying to print all data from array in look see below imageenter image description here it gives me error like this (see in image)enter image description here

How to print this data?

2 Answers2

0

By the logic, i will tell "i" go to far away, but to be sure to not have any error, juste check if newData.articles[i].source.name is defined in a "if", if it's true, you assigned to your variables and send the message, if not, just go next

Jonathan Delean
  • 1,011
  • 1
  • 8
  • 25
0

First Check the newData.articles[i].source is defined

Change Your for loop Like this

    for(let i =0;i<itr;i++){
      if(newData.articles[i]&&newData.articles[i].source){
        let name=newData.articles[i].source.name||"Noname",
        title=newData.articles[i].source.title||"No Title",
        description=newData.articles[i].description||"No Description";
        bot.sendMessage(chatId,`${name}\n\nTitle : ${title}\n\nDescription : ${description}`)
      }       
    }
Afsal Cp
  • 21
  • 1