简体   繁体   English

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

[英]Unable to use external API on Botpress (axios)

When trying to use axis to query an external Weather API, I get this error 尝试使用轴查询外部Weather API时,出现此错误

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

Here is my action for getTropicalCyclones {} (of course I have to hide my client ID and secret) 这是我对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()

So my question is, how do I import axios? 所以我的问题是, 如何导入axios? I've tried 我试过了

const axios = require('axios') 

or import axios from 'axios'; 或从“ axios”导入axios;

but this causes a different error: 但这会导致另一个错误:

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

Looking at the package.json on GitHub, it looks like axios is already installed https://github.com/botpress/botpress/blob/master/package.json 查看GitHub上的package.json,看来axios已安装https://github.com/botpress/botpress/blob/master/package.json

However, I cannot locate this package.json on my bot directory... 但是,我无法在我的机器人目录中找到这个package.json ...

Secondly, based on an old version doc it looks like this example code just used axios straight https://botpress.io/docs/10.31/recipes/apis/ 其次,基于旧版本的文档,该示例代码看起来像是直接使用axios的示例代码https://botpress.io/docs/10.31/recipes/apis/

How do I use axios on Botpress? 如何在Botpress上使用axios? Any leads would be appreciated 任何线索将不胜感激

Botpress: v11.0.0 Botpress:v11.0.0

Simply use ES6 import . 只需使用ES6 import include this line at the top of your code. 在代码顶部添加此行。

import axios from 'axios';

Note : I'm expecting that the axios is already installed 注意 :我期望axios已经安装

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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