无法在Botpress(axios)上使用外部API

时间:2018-11-28 07:02:40

标签: javascript axios

当尝试使用轴查询外部Weather API时,出现此错误

ReferenceError: axios is not defined
at getTropicalCyclones (vm.js:16:9)

这是我为getTropicalCyclones {}做的事情 (当然,我必须隐藏我的客户ID和机密)

const getTropicalCyclones = async () => {

    const BASE_WEATHER_API = `https://api.aerisapi.com/tropicalcyclones/`
    const CLIENT_ID_SECRET = `SECRET`
    const BASIN = `currentbasin=wp`
    const PLACE = `p=25,115,5,135` // rough coords for PH area of responsibility
    const ACTION = `within` // within, closest, search, affects or ''
    try {
        let text = ''
        let response = {}
        await axios.get(
            `${BASE_WEATHER_API}${ACTION}?${CLIENT_ID_SECRET}&${BASIN}&${PLACE}`
    )
        .then((resp) => {]
            response = resp
            text = 'Success retrieving weather!'
        })
        .catch((error) => {
            console.log('!! error', error)
        })

    const payload = await bp.cms.renderElement(
        'builtin_text',
        {
            text,
        },
        event.channel
    )
    await bp.events.replyToEvent(event, payload)
} catch (e) {
    // Failed to fetch, this is where ReferenceError: axios is not defined comes from
    console.log('!! Error while trying to fetch weather info', e)
    const payload = await bp.cms.renderElement(
        'builtin_text',
        {
            text: 'Error while trying to fetch weather info.',
        },
        event.channel
    )
    await bp.events.replyToEvent(event, payload)
  }
}

return getTropicalCyclones()

所以我的问题是,如何导入axios?我已经尝试了

const axios = require('axios') 

或     从“ axios”导入axios;

但这会导致另一个错误:

Error processing "getTropicalCyclones {}"
Err: An error occurred while executing the action "getTropicalCyclones"

在GitHub上查看package.json,看起来axios已经安装 https://github.com/botpress/botpress/blob/master/package.json

但是,我无法在我的bot目录中找到这个package.json ...

第二,基于旧版本的文档,该示例代码看起来像是直接使用axios的代码 https://botpress.io/docs/10.31/recipes/apis/

如何在Botpress上使用axios? 任何线索将不胜感激

Botpress:v11.0.0

1 个答案:

答案 0 :(得分:0)

只需使用ES6 import。 在代码顶部添加此行。

import axios from 'axios';

注意:我期望axios已经安装