skip to Main Content

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

Answers


  1. 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

    Login or Signup to reply.
  2. 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}nnTitle : ${title}nnDescription : ${description}`)
          }       
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search