简体   繁体   中英

Console logging a response from an API printing undefined

This is for an assignment to create a Jeopardy game using the jService api; I am walking through and console.logging to check myself, but when I log the answer below it prints undefined, while the question and title print to the console without issue. What am I missing?

async function getCategory() {
    for (let catID of catIDs) {
        // console.log(catID);
        const response = await axios.get('http://jservice.io/api/clues?category=' + catID);
        // console.log(response);
        let clueArray = [
            {
                question: response.data[0].question
            },
            {
                answer: response.data[0].answer
            }      
        ];

        let clue = {
            title : response.data[0].category.title,
            cluesArray: clueArray
        }
        console.log(clue.title);
        console.log(clue.cluesArray[0].question);
        console.log(clue.cluesArray[0].answer);    //this line logs undefined      
    }
    return clue;
}
let clueArray = [
    {
        question: response.data[0].question
    },
    {
      answer: response.data[0].answer
    }      
];

should be

let clueArray = [
  {
    question: response.data[0].question,
    answer: response.data[0].answer
  }    
];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM