TypeError:无法读取未定义的属性“数据”

时间:2018-11-23 14:55:52

标签: javascript botframework axios

在我的机器人程序中,我有一个区域,用户可以在其中使用关键字搜索知识文章。知识文章存储在“立即服务”表中。该漫游器应该以轮播格式返回所有与该关键字匹配的文章,但是每当我单击搜索按钮时,都不会发生。下面的代码:

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

我接受了axios请求,并将其放入与机器人完全分开的.js文件中,以确保该请求能够找到结果。下面的代码:

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()

运行此.js文件时,它失败并显示以下错误:

TypeError: Cannot read property 'data' of undefined

知道我的请求做错了什么吗?

0 个答案:

没有答案