控制台记录来自未定义的API打印的响应

时间:2020-04-17 02:38:58

标签: javascript api

这是一项使用jService api创建“危险游戏”的任务;我正在遍历console.logging进行自我检查,但是当我在下面记录答案时,它的打印内容未定义,而问题和标题将打印到控制台而没有任何问题。我想念什么?

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;
}

1 个答案:

答案 0 :(得分:0)

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

应该是

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