TypeError: Cannot read property 'data' of

2019-08-01 01:30发布

问题:

In my bot I have a section where the user can search for knowledge articles using a keyword. The knowledge articles are stored in a service now table. The bot is supposed to return all of the articles that match the keyword in a carousel format but whenever I click the search button nothing happens. Code below:

const search = async (turnContext) => {
const knowledgeBaseTopic = turnContext.activity.value.knowledgeBaseTopic;
const message = await new Promise(resolve => {
    axios.request({
        url: `url + topic`,
        method: 'get',
        baseURL: 'url',
        auth: {
            username: 'username',
            password: 'password'
        }
    },
    (error, response, body) => {
        console.log(error);
        var stuff = [];
        let messageWithCarouselOfCards = MessageFactory.carousel(stuff);
        for (var i = 0, len = body.result.length; i < len; i++) {
            stuff.push(
                CardFactory.heroCard(body.result[i].short_description, ['imageUrl1'], [`${ process.env.SN_KB_Resp_URl }${ body.result[i].number }`])
            );
        }
        resolve(messageWithCarouselOfCards);
    });
});
return turnContext.sendActivity(message);
};

I took my axios request and put it in a .js file completely seperate to the bot to ensure that the request was finding results. Code below:

const axios = require('axios')

const getArticles = async () => {
  try {
    axios.request({
      url: 'url',
      method: 'get',
      baseURL: 'url',
      auth: {
            username: 'username',
            password: 'password'
          },
        }
      )
    } 
  catch (error) {
    console.error(error)
  }
}

const countArticles = async () => {
  try{
  let articles = await getArticles()
    console.log(`${Object.entries(articles.data.message).length}`)
  } catch (error) {
      console.error(error)
    }
}

countArticles()

When running this .js file it fails with the error:

TypeError: Cannot read property 'data' of undefined

Any idea what I'm doing wrong with my request?